UISearchController overlaps with status bar and doesn't fill iPhone X
Asked Answered
S

1

6

I encountered a lot of issues with iOS 11 and displaying UISearchController by presenting it over navigation bar (as described here, example from Apple tutorials)

@IBAction func searchAction(sender: UIBarButtonItem) {
    // Create the search controller and specify that it should present its results in this same view        
    searchController = UISearchController(searchResultsController: nil) 

    // Make this class the delegate and present the search
    self.searchController.searchBar.delegate = self
    presentViewController(searchController, animated: true, completion: nil)
}

It is hiding app's UINavigationBar and displaying UISearchController with search bar.

Issue 1. On iOS 11 it is causing search bar to overlap with status first time it appears (it is not overlaping after trying again).

enter image description here UISearchController presented for the first time. No space between status bar and search bar.

enter image description here UISearchController presented again, UINavigationBar is bigger and search bar is way lower status bar.

Issue 2 On iPhone X it is not covering the whole space when presented

enter image description here

I have spend hours trying to figure it out. Is there other, simply way to show search bar on iOS 11 after clicking eg. search icon in navigation bar? Is there a way to fix UISearchController navigation bar height and space on iPhone X?

Sambo answered 13/10, 2017 at 12:34 Comment(3)
Actually, the native Calendar app's search has the same behavior. So I'm not sure if this is the bug. However I would like to have workaround to Issue 2 you mentioned as well.Dogeared
Did you resolve this? I'm seeing the same issue. Indeed, Calendar app exhibiting same issue as well. Using the iOS 11 UINavigationItem.searchController is not an alternative since I do not want the search bar to be permanent on the navigation bar.Uprise
Has anyone got the solution to the above problem? I am facing the same issue.Imbecility
B
1

In Apple's "Building Apps for iPhone X" video, Apple suggests to use the searchController property of a UINavigationBar instead of presenting the search controller manually.

Here is how it works:

if #available(iOS 11, *) {
   self.navigationItem.searchController = searchController;
   searchController.isActive = true;
} else {
   self.present(searchController, animated: true, completion: nil)
}

Note that this is only available in iOS 11. For earlier versions, do whatever you were already doing as it will continue to work.

If you make the change above, you will likely have issues where your search controller takes up the entire screen. To fix this you can set the 'definesPresentationContext' property on the primary UIViewController you are displaying the UISearchController from:

//set up UISearchController
self.definesPresentationContext = true;

If you end up setting the definesPresentationContext property to true, make sure to check that it doesn't interfere with any other UIViewControllers your VC presents.

Burnsides answered 16/10, 2017 at 18:31 Comment(1)
If I do this, the search bar is permanently on my navigation bar (except during scroll) which I do not want. Assigning the controller on button press like you suggested looks fine but the dismissal navigationItem.searchController = nil looks terrible. Any ideas?Uprise

© 2022 - 2024 — McMap. All rights reserved.