Search code examples
iosswiftuisearchbaruisearchcontroller

Presenting UISearchController overlaps with statusbar


I have a UISearchController which I present programmatically when the user touches a button. My problem is that the search bar overlaps the status (see screenshot)

enter image description here

I have the following code which I use to present the UISearchController

func presentSearchController() {
    let resultsController = ResultsViewController()

    self.searchController = UISearchController(searchResultsController: resultsController)
    self.searchController.searchBar.searchBarStyle = .prominent

    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchResultsUpdater = resultsController

    self.definesPresentationContext = true

    self.present(self.searchController, animated: true, completion: nil)
}

Edit: My question is not a duplicate of UISearchBar overlaps status bar in iOS as I am not working directly with the searchbar or it's frame


Solution

  • I have solved the issue by remove the following line:

    self.definesPresentationContext = true

    The reason this is not required in my scenario is that I am calling self.present(self.searchController, animated: true, completion: nil)

    UPDATE:

    While the above did fix my issue, it broke the functionality of pushing a viewController in my navigationController while the SearchController was displayed (the searchController would stay on top while the viewController was pushed underneath it)

    Upon revisiting the issue, here are the fixes I made:

    Add the following

    searchController.hidesNavigationBarDuringPresentation = true self.definesPresentationContext = true

    Then in the storyboard enable "Under Top Bars" and enable "Under Opaque Bars"