Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController searchResultsController disappears when searchbar text is empty

I have a UISearchController that displays it's searchResultsController (which is another view controller) when the searchbar is tapped. I do this using this UISearchController delegate method:

-(void)presentSearchController:(UISearchController *)searchController {

    dispatch_async(dispatch_get_main_queue(), ^{
        searchController.searchResultsController.view.hidden = NO;
    });
}

However, any time the searchbar's text is empty, whether by manually deleting all text or tapping the little x button, that searchResultsController view is disappearing until I start typing text again. Any ideas why this may be happening? Is there another method or delegate method that is being triggered when searchbar.text is empty?

like image 836
Tony Dakhoul Avatar asked Oct 31 '22 02:10

Tony Dakhoul


1 Answers

So after fiddling around with this for a while yesterday, this is the solution I found that ended up working. Figured I'd post it in case anyone else has the same problem!

-(void)presentSearchController:(UISearchController *)searchController {

    //forces searchResultsController to appear when searchBar tapped
        dispatch_async(dispatch_get_main_queue(), ^{
            searchController.searchResultsController.view.hidden = NO;
        });
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    //Prevents searchController from disappearing
    if ([searchText isEqualToString:@""])
    {
        [self presentSearchController:self.searchController];
    }
}
like image 182
Tony Dakhoul Avatar answered Nov 15 '22 05:11

Tony Dakhoul