I'm having a problem when one of my views initiated. I'm trying to get the search bar to show up when the view is initiated, but it shows up when I start scrolling down.
This shows up when I click on it:
and I'm trying to get this to show up when the view is initiated, which currently only shows up only when I start scrolling:
This is the code I have to set the search controller up so far:
searchController.searchBar.scopeButtonTitles = ["Posts", "Users"]
searchController.searchBar.delegate = self
navigationController?.navigationItem.searchController = searchController
navigationController?.navigationItem.searchController?.searchBar.isHidden = false
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
searchController.searchBar.isHidden = false
searchController.searchBar.showsScopeBar = true
// searchController.hidesNavigationBarDuringPresentation = false
self.navigationController?.setNavigationBarHidden(false, animated: true)
navigationItem.searchController = searchController
// navigationController?.navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
I've tried lots of different ways to get the search bar to appear when clicked on but I haven't been successful. Any ideas?
You need to add this line:
navigationItem.hidesSearchBarWhenScrolling = false
And if you wan't to show if from the beginning and hide it when scrolling then you need to do this:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = false
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = true
}
}