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

1

7

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

Fundamentalism answered 21/9, 2017 at 23:11 Comment(7)
Show more about how the search controller is configured, please.Nuthatch
@Nuthatch let searchController = UISearchController(searchResultsController: nil) is all I had. I've included this line in the question.Fundamentalism
Does your main view (not shown in the screen shot) underlap the navigation bar?Nuthatch
@Nuthatch The vc is a plain table view controller nested in a navigation view controller with no further configurations. Let me put up a minimum demo in a gist real quick.Fundamentalism
No need. I can reproduce.Nuthatch
@Nuthatch updated screenshots and uploaded a git repoFundamentalism
Fixed it, see revised answer.Nuthatch
N
3

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.

Nuthatch answered 21/9, 2017 at 23:32 Comment(3)
It doesn't solve the root problem but it's a nice workaround. Thanks!Fundamentalism
I agree that it doesn't solve the underlying problem. You should file a bug with Apple.Nuthatch
self.edgesForExtendedLayout = [] This line worked for me!Refection

© 2022 - 2024 — McMap. All rights reserved.