14
votes

I am using UISearchController to display a search bar and results within a UITableView. I managed to set it up correctly, but when I search the results and then select one of the rows in the tableview, and push a new view controller to the navigation stack, I would expect the search bar to not be visible anymore. However, when I try this, the search bar from the first view controller is visible in the 2nd view controller:

    if (self.searchController == nil) {
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.scopeButtonTitles = @[];
    self.searchController.searchBar.delegate = self;

    self.tableView.tableHeaderView = self.searchController.searchBar;
}

One option is to call self.searchController setActive:NO] inside of didSelectRowAtIndexPath: but there isn't a way to do this without the distracting animation of bringing down the search bar each time the search results are selected from.

Does anyone have the same problem? Is there a way to tell UISearchController to hide the search bar when pushed? It worked fine when I was using UISearchDisplayController

1
Does this solution help?Praveen Gowda I V

1 Answers

37
votes

Put this in your caller's viewDidLoad:

Swift:

self.definesPresentationContext = true

Objective-C:

self.definesPresentationContext = YES;

This solved the problem for me.