bloemy bloemy - 1 year ago 170
iOS Question

UISearchController searchBar disappears on first click

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?


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];

Answer Source

Try to check the navigationBar.translucent property - it should be YES when UISearchController will present the searchBar or else will be UI bugs.

Update from @SiavA

The better solution is use the extendedLayoutIncludesOpaqueBars property of the UIViewController. If you using the opaque navigation bar just set it in the true for controller which will be show UISearchController (not for navigationController).


- (void)viewDidLoad {
    [super viewDidLoad];

    self.extendedLayoutIncludesOpaqueBars = !self.navigationController.navigationBar.translucent;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download