Berry Blue
Berry Blue

Reputation: 16544

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

Upvotes: 1

Views: 1524

Answers (2)

Fergal Mohan
Fergal Mohan

Reputation: 43

Just a quick additional caveat that the searchBar might still disappear on versions < iOS 11 unless you specify that you don't want it to hide the NavBar e.g.

    if (@available(iOS 11.0, *)) {
        self.navigationItem.searchController = self.mySearchController;
        self.navigationItem.hidesSearchBarWhenScrolling = YES;
    } else {
        // Fallback on earlier versions
        self.tableView.tableHeaderView = self.mySearchController.searchBar;       // show the SearchBar in TV header
        self.mySearchController.hidesNavigationBarDuringPresentation = NO;
    }

Upvotes: 2

Marco Boschi
Marco Boschi

Reputation: 2343

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.

Upvotes: 4

Related Questions