My app is iPad landscape, using autolayout.
The initial view has a table view that occupies only half of the screen (the other half is a MapKit view).
This view controller containing that table (and the map view) is the top view controller of the app's root navigation controller.
On view load, I instantiate the UISearchController
and add its search bar to the table view's header view, as is standard procedure.
If I tap the search bar and enter some text, results are displayed. If I cancel the search here, no problem.
If instead, I select one of the result rows, and push into the next screen, and pop back, the search bar is gone. I can restore it by calling again:
self.tableView.headerView = self.searchController.searchBar
in viewDidAppear(), but that only works until I tap the search bar's 'cancel' button (at which point, it disappears again - right after the scope button collapse animation ends).
I have seen very many similar questions, but none describes my exact symptoms, and none of the solutions (or anything else I could think of):
// In viewDidLoad():
self.definesPresentationContext = true
self.searchController.definesPresentationContext = true
self.extendedLayoutIncludesOpaqueBars = true
self.navigationController?.extendedLayoutIncludesOpaqueBars = true
self.navigationController?.navigationBar.translucent = true
...seem to work in my case.
The weirdest part is, I have other similar table views with search interfaces (presented in modal view controllers), with no difference I can spot with respect to configuration, and those work alright...
I know I must be missing something...
After several hours of trying everything, I found the offending code:
func searchBarTextDidEndEditing(searchBar: UISearchBar)
{
// Dismiss the keyboard
self.resultSearchController.resignFirstResponder()
// Reload of table data
self.resultSearchController.loadView()
}
After commenting these two lines out -or better, the whole method (since I'm not doing anything else in it)- the problem sorted itself out.
NOTE: One of my coworkers implemented that method while trying different things to get the search to work as expected, and it somehow stuck...