Search code examples
iosswiftxcodeuisearchbaruisearchcontroller

Dark UISearchController iOS11


I am struggling really hard trying to reproduce this behaviour of the Files app on iPhone :

Beginning behaviour of UISearchController

End behaviour of UISearchController

So when we are clicking on the searchBar, the searchBar background stay white and the rest become dark.

But when I'm trying to reproduce this behaviour, I set this property to UISearchController:

searchController.obscuresBackgroundDuringPresentation = true

I also tried with this property :

searchController.dimsBackgroundDuringPresentation = true

But my whole view is dark..

If anyone have ideas, could be great :)

Thanks in advance!


Solution

  • On the UIViewController where you create your UISearchController. You need to set

    definesPresentationContext = true

    Otherwise, UIKit will infer the presentation context to be the root most view controller (for you likely your UINavigationController) and present the searchResultController on it causing it to cover top and bottom navigation bars.