Search code examples
iosswiftuisearchbaruisearchcontroller

UISearchBar gets cut off when using a UINavigationController inside a UITabBarController


I am trying to implement a search bar within one of the tabs in my UITabBarController, the tab is a UITableViewController within a UINavigationController...I am following an Apple tutorial - I have tried a lot of different options including answers mention here

Search bar gets cut off when using UISearchController with a UITabBarController inside a UINavigationController

I have tried setting the following property using

self.definesPresentationContext = true

or

self.tabBarController?.definesPresentationContext = true

Here is my code (from UITableViewController containing UISearchBar):

/// Search controller to help us with filtering.
    var searchController: UISearchController!

/// Secondary search results table view.
    var resultsTableController: SearchResultsTableController!

override func viewDidLoad() {
    super.viewDidLoad()
    resultsTableController = SearchResultsTableController()
    resultsTableController.tableView.delegate = self

    searchController = UISearchController(searchResultsController: resultsTableController)
    searchController.searchResultsUpdater = self
    searchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = searchController.searchBar

    searchController.delegate = self
    searchController.dimsBackgroundDuringPresentation = true
    searchController.searchBar.delegate = self  // so we can monitor text changes

    self.definesPresentationContext = true
}

Here's an image of the searchbar:

enter image description here

And once I tap it: enter image description here


Solution

  • Ok, finally solved the issue. This line got it to work

    self.extendedLayoutIncludesOpaqueBars = true 
    

    My TabBar isn't translucent so I didn't think that would make a difference but I set that on my UITableviewcontroller (the controller that is displaying the UISearchController) and now search displays in the navbar correctly. I also had extend edges under top & bottom bars set to true (using Interface Builder)