Albert Bori
Albert Bori

Reputation: 10002

Gap above UISearchBar using UISearchController

I'm experiencing an odd behavior with the UISearchBar in iOS. My search bar was working fine:

Good UISearchBar

But when I set UINavigationBar.appearance().translucent = false in my ApplicationDelegate's didFinishLaunchingWithOptions to get the colors right in the Navigation Bar in my iOS app, the search bar gets a white section above it like this:

Broken UISearchBar

When scrolling the results table view, the underlying content is visible in that gap:

Broken UISearchBar translucent

The closest I've gotten to fixing it, was setting mySearchResultsViewController.edgesForExtendedLayout = UIRectEdge.None, which stopped the translucent gap, but still doesn't have the right color:

Broken UISearchBar edgesForExtendedLayout None

Is there something I can do to prevent it from showing that discolored gap and still have globally opaque navigation bars?

Upvotes: 5

Views: 694

Answers (1)

Albert Bori
Albert Bori

Reputation: 10002

After much googling, I found the following answer buried in the search results: UISearchController doesn't work properly with a non-translucent UINavigationBar

Specifically, this snippet worked when I put it in the presenting view controller:

func viewDidLoad() {
    extendedLayoutIncludesOpaqueBars = true
}

So simple, yet difficult to guess.

Upvotes: 6

Related Questions