5

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

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不存在时,导航栏不会隐藏,搜索栏保持在其原始位置。但是,在存在该行的情况下,iPhone 和 iPad 上的竖屏模式和加号范围按钮都NavigationBar被隐藏了searchBar,但在横屏模式下保持不变(即使范围按钮很多并且不能放在一个线)。

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

谢谢!

4

1 回答 1

8

好的,在我研究另一个相关问题时找到了问题的原因,即searchBariOS 11 中的范围按钮和范围按钮未对齐。关键是 iOS 11 中的 searchController 配置方案已更改,searchBar不应再显示为相反,整个tableView's headerViewsearchController 应该是 的一部分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 2017 视频中推荐的,但我多么希望 Xcode 可以在旧的 tableHeaderView 分配行给我们一个警告,它会救我可能还有很多其他开发人员的困惑和研究时间。

于 2017-09-26T21:24:24.047 回答