Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 473
Roval Avatar asked Oct 13 '17 12:10

Roval


1 Answers

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.

like image 112
Brad Hesse Avatar answered Oct 20 '22 16:10

Brad Hesse