Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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];
like image 406
el-flor Avatar asked Oct 26 '15 08:10

el-flor


2 Answers

This happened to me when the UISearchController was hiding the navigation bar. Setting this property fixed it:

UISearchController.hidesNavigationBarDuringPresentation = NO;

like image 123
MattGerg Avatar answered Dec 05 '22 01:12

MattGerg


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).

E.g.

- (void)viewDidLoad {
    [super viewDidLoad];

    self.extendedLayoutIncludesOpaqueBars = !self.navigationController.navigationBar.translucent;
}
like image 30
Serge Maslyakov Avatar answered Dec 05 '22 00:12

Serge Maslyakov