将以下代码放入viewDidLoad方法中,问题得到了解决:
self.edgesForExtendedLayout = UIRectEdgeNone;
self.edgesForExtendedLayout = UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight;
可能是一个更好且更通用的解决方案。(这将保留其他边缘的扩展布局,对于具有屏幕底部模糊效果的工具栏等内容非常重要。) - smileyborgif ([self respondsToSelector:@selector(edgesForExtendedLayout)]){self.edgesForExtendedLayout = UIRectEdgeNone;}
。 - richy感谢hodade将我引向正确的方向!你的解决方案有效,但它只移动了搜索栏的框架,其他子视图仍停留在错误的位置。我所做的唯一更改就是将我的所有子视图移动到我的视图中,并进行动画处理。
谢谢!
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
[UIView animateWithDuration:0.25 animations:^{
for (UIView *subview in self.view.subviews)
subview.transform = CGAffineTransformMakeTranslation(0, statusBarFrame.size.height);
}];
}
}
-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
[UIView animateWithDuration:0.25 animations:^{
for (UIView *subview in self.view.subviews)
subview.transform = CGAffineTransformIdentity;
}];
}
}
你可能正在使用非半透明的导航栏? 如果是这样,这将解决问题。
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
self.navigationController.navigationBar.translucent = YES;
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
self.navigationController.navigationBar.translucent = NO;
}
只需将以下代码放置在-(void) ViewDidLoad中。它适用于iOS 7及更高版本。
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
更新:
if(SYSTEM_VERSION_GREATER_THAN(@"6.1")) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
- (void) viewDidLayoutSubviews
{
if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
CGRect viewBounds = self.view.bounds;
CGFloat topBarOffset = self.topLayoutGuide.length;
viewBounds.origin.y = topBarOffset * -1;
self.view.bounds = viewBounds;
}
}
这似乎描述了我遇到的问题,希望它能帮助我的前任。
Subclass your SearchDisplayController that's been added to your UIViewController/UITablewViewController,
Add something like this to its implementation:
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
CGRect frame = self.searchResultsTableView.frame;
frame.origin.y = CGRectGetHeight(self.searchContentsController.navigationController.navigationBar.frame);
frame.size.height = CGRectGetHeight(frame) - CGRectGetMinY(frame);
self.searchResultsTableView.frame = frame;
frame = self.searchBar.frame;
self.searchBar.frame = frame;
[self.searchContentsController.view insertSubview:self.searchBar aboveSubview:self.searchResultsTableView];
}
我认为将以下代码添加到viewDidLoad中可能会有帮助:
if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
只需添加
self.definesPresentationContext = YES;
在我的情况下,搜索栏下面的视图放置正确,只有搜索栏重叠在状态栏上。在这种情况下,下面的代码可以正常工作:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect frame = self.searchBar.frame;
frame.origin.y += statusBarFrame.size.height;
self.searchBar.frame = frame;
}
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect frame = self.searchBar.frame;
frame.origin.y -= statusBarFrame.size.height;
self.searchBar.frame = frame;
}
}
希望这对其他人有用
对已添加到您的UIViewController/UITablewViewController
中的SearchDisplayController
进行子类化,并将以下内容添加到其实现中 -
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if(self.active == visible) return;
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
if (visible) {
[self.searchBar becomeFirstResponder];
} else {
[self.searchBar resignFirstResponder];
}
}