更新到iOS11后,我的searchBar表现得非常奇怪。激活后,它会跳转到屏幕顶部。它像以前一样工作,但我当然希望它留在原地。我通过谷歌搜索这种行为尝试了很多解决方案,但没有任何帮助。有没有人有同样的问题?你做了什么来解决这个问题?
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
// Setup the Search Controller
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.preservesSuperviewLayoutMargins = true
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
walkaboutTableView.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = matildaLightBlue
searchController.searchBar.clipsToBounds = true
searchController.searchBar.layer.borderWidth = 2
searchController.searchBar.layer.borderColor = UIColor.black.cgColor
}
答案 0 :(得分:0)
将搜索控制器实例添加到导航项而不是表头视图中有助于我解决项目中的类似问题。
if #available(iOS 11, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
答案 1 :(得分:0)
Ruslan Kolosovskyi的回答非常好。此外,如果您希望搜索栏更易于访问:
if #available(iOS 11, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
tableView.tableHeaderView = searchController.searchBar
}