Search Bar in a Navigation Item collapses and gets stuck under status bar upon navigation pop, on iOS 11

我与影子孤独终老i 提交于 2019-12-07 04:15:39

问题


I am using the new iOS 11 searchContoller property of a UINavigationItem. I am running iOS 11.0 GM build.

When I perform a push segue whilst the search controller is active, it works fine. When I subsequently pop back, the search bar is collapsed and squashed up in the status bar. I cannot then cancel the search, or edit the search text.

See the following sequence of images:

The final image is showing the appearing of the table during the pop segue to return from a presented view controller back to the table with the search bar. Strangely, this doesn't always happen. It happens 90% of the time, but sometimes it behaves fine. I haven't yet worked out what I am doing differently to make it work. Once the search bar is squashed, I have to force close the app to get back to a sensible state.

The code which sets up the search controller is pretty standard. The relevant bit of viewDidLoad() is as follows:

searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.returnKeyType = .done
searchController.searchBar.placeholder = "Your Library"
searchController.searchBar.searchBarStyle = .minimal

// We will manage the clearing of selections ourselves.
clearsSelectionOnViewWillAppear = false

// Some search bar styles are slightly different on iOS 11
if #available(iOS 11.0, *) {
    navigationItem.searchController = searchController
    navigationController!.navigationBar.prefersLargeTitles = true
}
else {
    searchController.searchBar.backgroundColor = tableView.backgroundColor!
    searchController.hidesNavigationBarDuringPresentation = false
    tableView.tableHeaderView = searchController.searchBar
    tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.height), animated: false)
}

I've also noticed this issue in Apple's Messages app (see screenshot below), along with Settings, Notes and Mail, so presumably this is an iOS 11 bug.

This only seems to happen when using smaller than default Text Size in Settings -> General -> Accessibility -> Larger Text, and only seems to happen on a physical device (haven't reproduced it in the simulator). In viewDidAppear, searchController.searchBar.frame.height is equal to 0 (but not in viewDidDisappear, not viewWillAppear). The only workaround I have so far is:

override func viewDidAppear(_ animated: Bool) {
    if #available(iOS 11.0, *), searchController.searchBar.frame.height == 0 {
        navigationItem.searchController?.isActive = false
    }

    super.viewDidAppear(animated)
}

Is there a better way to get around this problem?


回答1:


This bug can be reproduced in iOS 11.1:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.hidesSearchBarWhenScrolling = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if (@available(iOS 11.0, *)) {
        self.navigationItem.hidesSearchBarWhenScrolling = YES;
    }
}

Avoiding mutate navigationItem on VC lifecycle events, fixed problem for me



来源:https://stackoverflow.com/questions/46239530/search-bar-in-a-navigation-item-collapses-and-gets-stuck-under-status-bar-upon-n

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