UISearchBarController iOS 11 issue - SearchBar and scope buttons overlap

后端 未结 7 868
误落风尘
误落风尘 2021-01-04 07:26

Referred here and here. No answer in first link. In the second link, though the answer is not accepted, but the link to apple developer forum gives error.

Before i

7条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 07:35

    I can get the initial appearance to display correctly in iOS11 using the following code (as per greg's answer):

    [self.searchController.searchBar sizeToFit];
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.searchController = self.searchController;
        self.navigationItem.hidesSearchBarWhenScrolling = NO;
    } else {
        // Fallback on earlier versions
        self.tableView.tableHeaderView = self.searchController.searchBar;
    }
    

    However, if the app is backgrounded then restored while the search bar was active, the appearance would end up overlapped as shown in Nitish's second screenshot above.

    I was able to fix that with the following workaround:

    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        self.searchController.searchBar.showsScopeBar = NO;
        [self.searchController.searchBar sizeToFit];
        self.searchController.searchBar.showsScopeBar = YES;
        [self.searchController.searchBar sizeToFit];
    }];
    

    (I'm still working on how to workaround the layout issues following an interface orientation change while the search bar is active - that still ends up overlapped.)

提交回复
热议问题