我的应用程序中有一个搜索控制器,根据iOS 11上的苹果文档,必须将其添加到navigationItem的searchController属性中。我还有一些searchController的范围按钮。一切正常,但是当我将设备旋转到横向并旋转回肖像时,就会发生有线的事情。范围按钮隐藏在搜索字段下。如果我按下取消按钮并再次点击搜索字段,则会返回正常状态。该错误仅在iOS 11上每次都会重现。搜索栏将如下所示:
这就是我添加searchController的方式:
searchController.searchResultsUpdater = self as UISearchResultsUpdating
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.tintColor = UIColor.black
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = "Search device"
searchController.searchBar.scopeButtonTitles = ["Device", "Person", "OS version", "Serial"]
if #available(iOS 11.0, *) {
self.navigationItem.searchController = self.searchController
self.navigationItem.hidesSearchBarWhenScrolling = false
if let textField = searchController.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundView = textField.subviews.first {
backgroundView.backgroundColor = UIColor.white
backgroundView.layer.cornerRadius = 10
backgroundView.clipsToBounds = true
}
}
} else {
tableView.tableHeaderView = searchController.searchBar
}
searchController.searchBar.delegate = self as UISearchBarDelegate