swiftuinavigationitemuisearchcontrolleruisearchresultscontroller

SearchResultsController appearing over UINavigationBar


I'd like to have a separate viewController as the searchResultsController however, I am getting unusual behaviour illustrated below.

I am setting up searchController as such;

func setUpSearchControllerAttributes() {
    searchController = UISearchController(searchResultsController: searchResultsController)
    searchController.delegate = searchResultsController
    searchController.definesPresentationContext = true
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.sizeToFit()
    searchController.searchBar.backgroundColor = .white
    searchController.searchBar.barTintColor = .white
    searchController.searchBar.placeholder = "Search"
    searchController.searchBar.searchBarStyle = .minimal
}

When I add:

navigationItem.titleView = searchController.searchBar

I get:

navigationItem.titleView = searchController.searchBar

I have set the searchResultsController view colour to .green with a low alpha for illustrative purposes.

As you can see the searchResultsController is overlapping the navigationBar.

Now when I set:

navigationItem.searchController = searchController

I get the desired result below:

navigationItem.searchController = searchController

Is there any explaination for this. Obviously the problem lies with setting the searchResultsController SearchBar as the navigationItem TextView.


Solution

  • I had;

    searchController.definesPresentationContext = true
    

    Which needed to be replaced by;

    self.definesPresentationContext = true