Search code examples
iosswiftuisearchcontroller

UISearchController in navigationBar


I'm trying to use the new UISearchController in my tableViewController.

However I'm a bit confused on how it can move up in the navigationController when I press the searchBar like it did with the old searchDisplayController?

At the moment it just stay in the tableHeader.

Here is my code:

    self.teamSearchController = ({
        let controller = UISearchController(searchResultsController: nil)

        controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
       controller.searchBar.showsScopeBar = true
        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

Controller:

enter image description here

When I click on searchbar:

enter image description here


Solution

  • You can place the UISearchBar of UISearchController in the navigation bar instead of table header

    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    
    // Include the search bar within the navigation bar.
    self.navigationItem.titleView = self.searchController.searchBar;
    
    self.definesPresentationContext = YES;