0

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.

David Henry
  • 1,972
  • 20
  • 43

1 Answers1

4

I had;

searchController.definesPresentationContext = true

Which needed to be replaced by;

self.definesPresentationContext = true
David Henry
  • 1,972
  • 20
  • 43