Peter Pik Peter Pik - 7 months ago 26
Swift Question

UISearchController in navigationBar

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

However I'm a bit confused on how I when I press the searchBar it can move up in the navigationController exactly how 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

Answer

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;
Comments