Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController.hidesNavigationBarDuringPresentation ignored with scopeButtons in iOS 11 Beta

In our project we specified that

hidesNavigationBarDuringPresentation = false

on a particular UIViewController's UISearchController. The searchController has an array of scope titles. This so far works fine up to iOS 10, but in iOS 11 betas, it looks like the false setting of hidesNavigationBarDuringPresentation is ignored and messes up our display. To make sure it's not because of other factors in my project, I created a bare-bone test project with just a single UITableViewController, with a UISearchController initialized with another simple UITableViewController. The following code is in the viewDidLoad() method on the main view controller:

    self.title = "Search Bar Scope Test"
    let searchViewController = SearchViewController(style: .plain)
    searchController = UISearchController(searchResultsController: searchViewController)
    searchController!.searchBar.sizeToFit()
    tableView.tableHeaderView = searchController!.searchBar
    searchController?.hidesNavigationBarDuringPresentation = false

    searchController?.searchBar.scopeButtonTitles = ["scope 1", "scope 2", "scope 3", "scope 4", "scope 5"]

When the last line assigning scopeButtonTitles is not present, the navigation bar didn't get hidden and the search bar remains in its original position. However, with that line present, the NavigationBar becomes hidden and the searchBar plus the scope buttons all moved up in portrait mode on both iPhone and iPad, but stays the same in landscape mode (even if the scope buttons are many and can't fit in one line).

Did anybody else encounter this? Is this a bug or expected behavior (certainly not our desired behavior though) in iOS 11, and is there any workaround?

Thanks!

like image 292
CodeBrew Avatar asked Sep 11 '17 20:09

CodeBrew


1 Answers

OK, found the cause of the problem while I was researching another related issue, that the searchBar and the scope button got mis-aligned in iOS 11. The key is that the searchController configuration scheme has changed in iOS 11 where searchBar should no longer be presented as the tableView's headerView, instead, the whole searchController should be part of the navigationItem, as illustrated below:

if #available(iOS 11.0, *) {
    self.navigationItem.searchController = searchController
    // optional, but apparently due to a bug in iOS 11, 
    // the searchBar and the scope buttons may get too high and mis-aligned
    // when the nav bar is hidden
    searchController?.hidesNavigationBarDuringPresentation = false
} else {
    tableView.tableHeaderView = searchController!.searchBar
}

The above code fixed a few UI problems I had related to UISearchBar in iOS 11, and is actually recommended in this WWDC 2017 video, but how I wish if Xcode could give us a warning at the old tableHeaderView assignment line, it would have saved me and probably quite some other developers' confusion and research time.

like image 89
CodeBrew Avatar answered Oct 14 '22 12:10

CodeBrew