Search bar overlaps with status bar on iOS 11

风流意气都作罢 提交于 2019-12-04 17:34:15

问题


I am using a UISearchController and a UISearchResultsController to implement search functionality.

MySearchResultsController implements UISearchResultsUpdating and UISearchBarDelegate:

override open func viewDidLoad() {
    super.viewDidLoad()
    self.edgesForExtendedLayout = [];
    self.automaticallyAdjustsScrollViewInsets = false;
}

I display the searchbar in the tableHeader like this in MyTableViewController:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
    self.searchController.searchResultsUpdater = self.searchResultsController;
    self.searchController.searchBar.delegate = self.searchResultsController;
    self.searchController.searchBar.scopeButtonTitles = @[NSLocalizedString(@"SEARCH_SCOPE_TEMPERATURES", nil), NSLocalizedString(@"SEARCH_SCOPE_KNOWHOW", nil)];
    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;
}

This worked perfectly before, but under iOS 11 the search bar overlaps with the status bar as soon as I tap into it (see screenshots). I tried lots of different things to get it to display correctly but haven't found a solution yet.


回答1:


I found that the problem was that the presenting view Controller also sets

override open func viewDidLoad() {
    super.viewDidLoad()
    self.edgesForExtendedLayout = [];
    self.automaticallyAdjustsScrollViewInsets = false;
}

I have to do this because the table view does not actually extend all the way to the top.

I solved this like that in my presenting view Controller:

override open func viewDidLoad() {
    super.viewDidLoad()
    self.automaticallyAdjustsScrollViewInsets = false;
    if (@available(iOS 11.0, *)) {
        //NSLog(@"iOS 11.0");
    } else {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        //NSLog(@"iOS < 11.0");
    }
}

Seems to be an iOS 11 bug, or at least an odd behavior…




回答2:


This is what Worked for me:

    override func viewDidLoad() {
        // to fix the Status Bar Issue:
        if #available(iOS 11.0, *) {
            definesPresentationContext = true
        }
       // You'll also need this properties on your Search Bar:
        searchController = UISearchController.init(searchResultsController: nil)
        searchController?.searchResultsUpdater = self
        searchController?.hidesNavigationBarDuringPresentation = false
    }



回答3:


I managed to solve this by subclassing UISearchController. My answer is in Swift but maybe the principles works with ojective-c as well. Please see my answer here: https://stackoverflow.com/a/46339336/8639272



来源:https://stackoverflow.com/questions/46295761/search-bar-overlaps-with-status-bar-on-ios-11

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