UISearchController searchResultsController disappears when searchbar text is empty

1.2k views Asked by At

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?

1

There are 1 answers

1
Tony Dakhoul On

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