Search bar overlapping status bar in search controller

喜欢而已 提交于 2019-12-13 03:37:43

问题


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;       
}

There should be more padding here with the status bar.

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


回答1:


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.




回答2:


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;
    }


来源:https://stackoverflow.com/questions/47503666/search-bar-overlapping-status-bar-in-search-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!