I have implemented a UISearchController in a TableView, pushed by a Navigation Controller.
First my problem was that whenever I click on the SearchBar, it disappears. It works when I enter some text, but it stays completely blank. Then I managed to semi solve the issue using this code:
- (void)searchForText:(NSString*)searchText
{
[self.view addSubview:villeSearchController.searchBar];
}
Which semi-works because now, when I click on the search bar, it blanks out, but if I enter one character, it appears again, and then it stays there, no matter what. Until I cancel the search, and click on it again, in that case it blanks out. I have made some tests and this method (searchForText) is called on the very first click, so that isn't the reason.
Does anyone know how I can solve this issue and make the searchbar appear from the very first click?
EDIT:
This is how I initialize the SearchController:
villeSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
villeSearchController.searchResultsUpdater = self;
villeSearchController.dimsBackgroundDuringPresentation = NO;
villeSearchController.searchBar.delegate = self;
villeTableView.tableHeaderView = villeSearchController.searchBar;
villeSearchController.searchBar.scopeButtonTitles = @[];
self.definesPresentationContext = YES;
[villeSearchController.searchBar sizeToFit];
Try to check the
navigationBar.translucent
property - it should be YES whenUISearchController
will present thesearchBar
or else will be UI bugs.Update from @SiavA
The better solution is use the
extendedLayoutIncludesOpaqueBars
property of theUIViewController
. If you using the opaque navigation bar just set it in thetrue
for controller which will be showUISearchController
(not fornavigationController
).E.g.
Place the SearchController inside a UIScrollView and it will work fine. This if you are using it in the section header or as a separate view
This happened to me when the
UISearchController
was hiding the navigation bar. Setting this property fixed it:UISearchController.hidesNavigationBarDuringPresentation = NO;
If you run into this problem in iOS11 (and especially if it worked pre iOS11), I had to change my UISearchController to be attached to the navigationItem rather than the tableView.
After setting parameters on my searchController, I used to do this:
Now I have this:
The "translucent" fix would allow the controller to appear, but when I would try and unwind to a specific segue, I'd get a crash. Attaching the searchController to the navigationItem fixed both the display and the crash.
Setting isHidden of the navigation bar to false stopped the search bar from disappearing for me.