iOS 7 UISearchDisplayControllersearch栏search时重叠状态栏

我正在更新我的iOS 7应用程序,并且正在调整所有视图以考虑新的透明状态栏(我的应用程序仍将使用不透明的导航栏)。

在每个视图中调整状态栏相对比较容易,除了我在一个视图控制器中将UISearchBar连接到UISearchDisplayController时遇到的一个主要问题。

search栏似乎正常显示,如下所示:

search栏http://imageshack.us/a/img163/9128/06vx.png

问题是,一旦我开始search,导航栏就会消失(因为它应该),但其他的一切都会向上移动,以重叠状态栏:

损坏的search栏http://imageshack.us/a/img11/8237/corh.png

这看起来没有像预期的那样工作,因为屏幕变暗发生在search栏下方20个像素处,search栏应该结束。

在iOS 7中是否有内置的解决scheme? 每次用户开始和结束search时,我宁愿不必为每个视图手动调整框架。

谢谢!

把下面一行放在viewDidLoad中对我进行了修复:

self.edgesForExtendedLayout = UIRectEdgeNone; 

谢谢hoda​​de带领我走上正轨! 你的解决scheme的工作,除了它只移动search栏的框架,把我的其他子视图在错误的地方。 我改变的唯一的事情是移动我的视图中的所有子视图,以及animation。

谢谢!

 -(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; } } 

这似乎描述了我所遇到的问题; 希望它能帮助我以前的位置。

  1. 子类你的SearchDisplayController被添加到你的UIViewController / UITablewViewController,

  2. 把这样的东西添加到它的实现中:

      - (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]; } 

我想也许join这个viewDidLoad将有助于:

 if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { self.edgesForExtendedLayout = UIRectEdgeNone; } 

只需添加

 self.definesPresentationContext = YES; 

你可以从这里阅读更多: UISearchController和definesPresentationContext

并从Apple文档: UISearchController文档

注意: UISearchDispalyController在iOS7中被弃用,在iOS8中使用UISearchController,上面的方法使用UISearchController

就我而言,search栏下方的视图位于正确的位置,只有search栏与状态栏重叠。 在这种情况下,这种代码的和平工作正常:

 - (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; } } 

希望对别人有用

 -(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; CGRect frame = controller.searchBar.frame; frame.origin.y += statusBarFrame.size.height; controller.searchBar.frame = frame; } } -(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; CGRect frame = controller.searchBar.frame; frame.origin.y -= statusBarFrame.size.height; controller.searchBar.frame = frame; } } 

以上的答案只有在你的导航栏不被隐藏的情况下才有效 。 如果您隐藏了导航栏,请尝试以下操作:

 -(void)viewDidAppear:(BOOL)animated{ if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; [self.tableView setFrame:CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y+statusBarFrame.size.height, self.tableView.frame.size.width, self.tableView.frame.size.height)]; } } 

基于这篇文章: UISearchBar重叠iOS中的状态栏

子类你的SearchDisplayController被添加到你的UIViewController/UITablewViewController并添加到它的实现 –

 - (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]; } }