UISearchController不能在旋转时重新显示导航栏
我已经实现了UISearchController,它工作的很好,除了…
当我点击search栏时,导航栏就像预期的那样很好地消失了。 当我将手机旋转到横向视图时,我可以得到这个有意义的视图。
但是,当我将手机旋转回纵向视图(仍在searchtypes区域中select)时,我会看到下面的视图。
你可以看到导航栏不会再出现。 我觉得我正在实施一个基本的search控制器。 什么可能导致这个?
self.venueSearchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.venueSearchController.searchResultsUpdater = self; self.venueSearchController.searchBar.delegate = self; self.venueSearchController.dimsBackgroundDuringPresentation = NO; self.venueSearchController.hidesNavigationBarDuringPresentation = YES; self.venueSearchController.searchBar.frame = CGRectMake(self.venueSearchController.searchBar.frame.origin.x, self.venueSearchController.searchBar.frame.origin.y, self.venueSearchController.searchBar.frame.size.width, 44.0); self.definesPresentationContext = YES; self.navigationController.navigationBar.translucent = YES; self.venueSearchController.searchBar.translucent = YES; self.tableView.tableHeaderView = self.venueSearchController.searchBar;
它看起来像UISearchController
忘记重置状态栏时重新显示searchBar
的框架。 我认为这可能是UISearchController
一个错误; 似乎有一些在雷达上市。 这似乎searchBar的超级查看(这是UISearchController内部)结束了错误的高度。 这是令人烦恼的,因为解决scheme因此涉及到searchController的视图层次结构,苹果可能会改变…您可能想要添加一个iOS版本的检查,因此它只运行指定的版本。
如果将以下代码添加到视图控制器中,则会在特征集合更改时调用它。 它会检查以查看a)search控制器是否处于活动状态b)statusBar未隐藏c)searchBar origin-y为0,如果是,则通过statusBar的高度增加超级视图的高度, searchBar下来。
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) { let app = UIApplication.sharedApplication() if searchController!.active && !app.statusBarHidden && searchController?.searchBar.frame.origin.y == 0 { if let container = self.searchController?.searchBar.superview { container.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y, container.frame.size.width, container.frame.size.height + app.statusBarFrame.height) } } }
目标C
- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection { [super traitCollectionDidChange: previousTraitCollection]; if(self.venueSearchController.active && ![UIApplication sharedApplication].statusBarHidden && self.venueSearchController.searchBar.frame.origin.y == 0) { UIView *container = self.venueSearchController.searchBar.superview; container.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y, container.frame.size.width, container.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height); } }
您可能需要在您的视图控制器中实现UIBarPositioningDelegate
。
self.venueSearchController.searchBar.delegate = self;
searchBar正在寻找来自它的委托的响应。
@protocol UISearchBarDelegate <UIBarPositioningDelegate>
添加以下内容到你self
(我认为它是一个ViewController)
#pragma mark - <UIBarPositioningDelegate> // Make sure NavigationBar is properly top-aligned to Status bar - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { if (bar == self.venueSearchController.searchBar) { return UIBarPositionTopAttached; } else { // Handle other cases return UIBarPositionAny; } }
我相信这可能是UITableViewController布局处理导航栏的方式的一个问题,而不是在您旋转的情况下出现导航栏。
我认为你可以通过将UITableViewControllerreplace成放入UITableView的UIViewController来解决这个问题。 然后将表视图的顶部约束设置为顶部布局指南。 将边界约束设置为0的左侧,右侧和底部。
这应该确保表格始终保持在状态栏的下方,并且在导航栏出现并返回时仍能正确移动。
看到这个讨论: iOS 7:UITableView显示在状态栏下
我通过closures设置中的“隐藏状态栏”来暂时“修复”这个问题。