Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation bar disappears when typing in UISearchController text field

I'm trying to figure out why my whole navigation bar disappears when I start typing in the UISearchController.searchBar It loads properly and animates properly, but I lose the active NavBar when I start typing. Here's the code to load the searchController from viewDidLoad:

    UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];

    searchResultsController.tableView.dataSource = self;
    searchResultsController.tableView.delegate = self;
    searchResultsController.tableView.backgroundColor = [UIColor redColor];    

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
    self.searchController.delegate = self;
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = YES;
    self.searchController.hidesNavigationBarDuringPresentation = NO;

    [self.searchController.searchBar sizeToFit];

    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = NO;

And here's the result after I start typing:

enter image description here

Notice how the table view is there, but it appears to occupy the space of the nav controller and extend downwards into the first section heading. Any ideas?

Thanks.

like image 722
4m1r Avatar asked Oct 29 '14 17:10

4m1r


1 Answers

You need to set definesPresentationContext to YES. From apples example:

// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.

self.definesPresentationContext = YES
like image 128
Jeremy Moyers Avatar answered Oct 21 '22 13:10

Jeremy Moyers