您好我希望searchBar
始终可见,这就是我所拥有的:
searchController = UISearchController(searchResultsController: nil) tableView.tableHeaderView = searchController.searchBar searchController.searchResultsUpdater = self searchController.dimsBackgroundDuringPresentation = false
有办法实现这个目标吗?
提前致谢
关于什么
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchController.searchBar.becomeFirstResponder()
}
?
看一下Apple的例子(源代码):https://developer.apple.com/library/content/samplecode/TableSearch_UISearchController/Introduction/Intro.html
注意 - 从iOS 11开始,你应该使用UINavigationController而不是表头视图:
if #available(iOS 11.0, *) {
// For iOS 11 and later, we place the search bar in the navigation bar.
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.searchController = searchController
// We want the search bar visible all the time.
navigationItem.hidesSearchBarWhenScrolling = false
} else {
// For iOS 10 and earlier, we place the search bar in the table view's header.
tableView.tableHeaderView = searchController.searchBar
}