14

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

Z S
  • 7,039
  • 12
  • 53
  • 105

1 Answers1

38

Put this in your caller's viewDidLoad:

Swift:

self.definesPresentationContext = true

Objective-C:

self.definesPresentationContext = YES;

This solved the problem for me.

Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
EricH206
  • 576
  • 4
  • 11
  • 5
    What an obscure solution. But sure enough, it worked! – Jacob Pritchett Oct 31 '15 at 19:28
  • 6
    In the `viewDidLoad` of WHAT? The current or the target controller. But doesn't work in both cases anyway. –  Oct 11 '17 at 11:18
  • 2
    I'm not sure what's more bizarre. The fact that it worked for you or the fact that it doesn't work for me. –  Jan 27 '18 at 12:28