Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 11 search bar has no top padding when status bar is hidden

When setting status bar to hidden

override var prefersStatusBarHidden: Bool {
    return true
}

and initialize a plain searchController using

let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController

It appears normal if unedited, however if you click into the search bar, the navigation title will hide and there is little padding between the search bar and top edge, which is very visually broken.

Solution is appreciated.

Minimum example

https://github.com/DJBen/SearchBarNoTopPadding

enter image description here

like image 805
Ben Lu Avatar asked Sep 21 '17 23:09

Ben Lu


1 Answers

This is a bug in iOS 11. To work around it, add these lines to your code:

    searchController.hidesNavigationBarDuringPresentation = false
    self.definesPresentationContext = true

The alternative is to go on doing this the old way, e.g. make the search controller's search bar your navigation item's titleView. That does still work fine in iOS 11.

like image 192
matt Avatar answered Sep 19 '22 17:09

matt