Search code examples
iosobjective-cuisearchcontroller

Search bar overlapping status bar in search controller


I created a UISearchController programmatically in a UITableViewController. It works fine but the search bar isn't displaying correctly with the status bar. Here is my code and some screenshots. It also makes a funny animation when canceling the search.

- (void)viewDidLoad
{
    [super viewDidLoad];

    _resultsTableViewController = [ResultsTableViewController new];
    _searchController = [[UISearchController alloc] initWithSearchResultsController:_resultsTableViewController];
    _searchController.searchResultsUpdater = _resultsTableViewController;
    _searchController.dimsBackgroundDuringPresentation = NO;
    self.definesPresentationContext = YES;
    self.tableView.tableHeaderView = _searchController.searchBar;       
}

enter image description here

There should be more padding here with the status bar. enter image description here

When you I cancel searching I get a bad animation here that's the height of the status bar. enter image description here


Solution

  • From your screenshots it appears you're working on iOS 11, with this version the way the UISearchController search bar is added to UI has changed. On iOS 11 is the navigation item that takes care of displaying search so UIKit has not been updated to correctly handle the search bar presented in the table header view.

    On iOS ≤10 you should continue to use

    self.tableView.tableHeaderView = _searchController.searchBar;
    

    but switch to

    self.navigationItem.searchController = _searchController;
    self.navigationItem.hidesSearchBarWhenScrolling = YES;
    

    on iOS 11 and later.