UISearchController.hidesNavigationBarDuringPresentation ignored with scopeButtons in iOS 11 Beta
Asked Answered
P

1

5

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!

Plataea answered 11/9, 2017 at 20:36 Comment(3)
I don't have a problem with the scope buttons, but my entire search bar jumps out of the table view and attaches to the top of the screen. Using Xcode 9 GM.Aurum
@ChrisPaveglio I finally found out the cause of the problem. See my answer below.Plataea
Thanks but that is not quite my issue. I need more time to figure it out b/c I think it's more unique. I have an iPad layout with 2 table views side by side and 1 has the search bar and it should never ever leave it's table view. I think I need to refactor some things before I go further.Aurum
P
10

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.

Plataea answered 26/9, 2017 at 21:24 Comment(1)
Huh, I've managed to continue using tableHeaderView for the searchBar until now and actually it still works fine this way when there are no scopeButtonTitles. Thanks!Eparch

© 2022 - 2024 — McMap. All rights reserved.