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

hidesNavigationBarDuringPresentation = false

在特定UIViewControllerUISearchController上。 searchController具有一系列作用域标题。到目前为止,它在iOS 10上都可以正常工作,但是在iOS 11 Beta中,似乎hidesNavigationBarDuringPresentation的错误设置被忽略,使我们的显示困惑。为了确保不是我项目中的其他因素,我创建了一个仅有一个UITableViewController的准系统测试项目,并使用另一个简单的UISearchController初始化了UITableViewController。以下代码位于主 View Controller 上的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的最后一行不存在时,导航栏没有隐藏,搜索栏保持在原始位置。但是,在存在该行的情况下,在iPhone和iPad上,NavigationBar变为隐藏状态,并且searchBar和合并范围按钮都在纵向模式下向上移动,但在横向模式下保持不变(即使合并范围按钮很多且无法容纳)在一行中)。

有人遇到过吗?这是iOS 11中的错误或预期行为(虽然不是我们的预期行为),并且有任何解决方法吗?

谢谢!

最佳答案

好的,当我研究另一个相关问题时,找到了问题的原因,即searchBar和scope按钮在iOS 11中未对齐。关键是iOS 11中的searchController配置方案已更改,其中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问题,实际上是在this WWDC 2017 video中推荐的,但是我希望Xcode能够在旧的tableHeaderView赋值行给我们一个警告,这会为我省去,并且也许其他一些开发人员的困惑和研究时间。

关于ios - iOS 11 Beta中的scopeButtons忽略了UISearchController.hidesNavigationBarDuringPresentation,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46163992/

10-14 20:14
查看更多