简体   繁体   中英

SearchBar hides under NavigationBar

I added a searchBar into a tableview. I also have a navigationbar and it should appear under it. It works fine until I tap into the searchbar. When I do so on devices with larger screen size (from iPhone 6) the searchbar hides under the navigationbar. It works as expected on an actual iPhone 5, 5s. This is how I initialize the searchBar:

    searchController.searchResultsUpdater = self as UISearchResultsUpdating
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchBar.tintColor = UIColor.black


    searchController.searchBar.scopeButtonTitles = ["Device", "Person", "OS version"]
    searchController.searchBar.delegate = self as UISearchBarDelegate

在我点击搜索栏之前

在点击searchBar之后

The searchBar should not hide because I need the segmented control on the navigationBar when I'm searching too. Do you have any idea how can I fix this problem?

Update: After setting the extended edges the searchBar looks like this: 导航栏颜色已更改

当我点击搜索栏

I solved the problem. It was an iOS 11 issue. According to wwdc recommendation you should add the searchBar for the navigationItem.

if #available(iOS 11.0, *) {
    self.navigationItem.searchController = self.searchController
    self.navigationItem.hidesSearchBarWhenScrolling = false

} else {
    tableView.tableHeaderView = searchController.searchBar
}

This solves the problem and it's working fine with any iOS type now. The searchBar will look like this on iOS 11: iOS 11的SearchBar

Select your viewController. Then in Utility Window you un tick the 1:- Under Top Bars

喜欢这张图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM