问题
I tried using Navigation Bar Large Title and adding Search Bar to Navigation Controller. The Search Bar is displayed under the Navigation title. It should be hidden until I swipe down. I understand that the default behavior is hidden unless I set "navigationItem.hidesSearchBarWhenScrolling = false". Even I add "navigationItem.hidesSearchBarWhenScrolling = true", it doesn't work.
I created a new app to test this function. It has ViewController and Navigation Controller embedded. Below is my code:
class ViewController: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.searchController = searchController
// navigationItem.hidesSearchBarWhenScrolling = true
}
}
回答1:
I have encountered with a similar situation. I've solved like this:
1- Remove everything about searchController
2- Add UISearchBar control into your table
3- Connect this searchBar control to a IBOutlet variable and set delegate
searchBar.delegate = self
4- Use only UISearchBarDelegate (remove UISearchResultsUpdating) and implement textDidChange method
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filter...
reload table...
}
this table header search bar works with iOS 11 and older versions
来源:https://stackoverflow.com/questions/46511382/searchbar-in-navigationbar-ios-11