iOS 11 Beta中的scopeButtons忽略了UISearchController.hidesNavigationBarDuringPresentation

在我们的项目中,我们指定了

hidesNavigationBarDuringPresentation = false 

在特定的UIViewControllerUISearchController 。 searchController有一个范围标题数组。 到目前为止,这在iOS 10中运行良好,但在iOS 11测试版中,看起来像hidesNavigationBarDuringPresentation的错误设置被忽略并且弄乱了我们的显示。 为了确保它不是因为我项目中的其他因素,我创建了一个只有一个UITableViewController的裸骨测试项目,其中一个UISearchController用另一个简单的UITableViewController初始化。 以下代码位于主视图控制器上的viewDidLoad()方法中:

  self.title = "Search Bar Scope Test" let searchViewController = SearchViewController(style: .plain) searchController = UISearchController(searchResultsController: searchViewController) searchController!.searchBar.sizeToFit() tableView.tableHeaderView = searchController!.searchBar searchController?.hidesNavigationBarDuringPresentation = false searchController?.searchBar.scopeButtonTitles = ["scope 1", "scope 2", "scope 3", "scope 4", "scope 5"] 

如果不存在指定scopeButtonTitles的最后一行,则导航栏不会被隐藏,搜索栏将保持其原始位置。 但是,在该行存在的情况下, NavigationBar变为隐藏状态,搜索searchBar和范围按钮在iPhone和iPad上都以纵向模式向上移动,但在横向模式下保持不变(即使示波器按钮很多且不适合在一行)。

有没有人遇到过这个? 这是iOS 11中的错误或预期行为(当然不是我们想要的行为),是否有任何解决方法?

谢谢!

好的,在我研究另一个相关问题时找到问题的原因,搜索searchBar和范围按钮在iOS 11中未对齐。关键是在iOS 11中searchBar配置方案已经改变,其中searchBar应该不再是作为tableView's headerView ,相反,整个searchController应该是navigationItem一部分,如下图所示:

 if #available(iOS 11.0, *) { self.navigationItem.searchController = searchController // optional, but apparently due to a bug in iOS 11, // the searchBar and the scope buttons may get too high and mis-aligned // when the nav bar is hidden searchController?.hidesNavigationBarDuringPresentation = false } else { tableView.tableHeaderView = searchController!.searchBar } 

上面的代码解决了我在iOS 11中与UISearchBar相关的一些UI问题,实际上是在这个WWDC 2017video中推荐的,但是我希望如果Xcode能够在旧的tableHeaderView分配行给我们一个警告,它会救我可能还有其他一些开发人员的困惑和研究时间。