Fixed UISearchBar using UISearchController - Not using header view of UITableView

MTRL picture MTRL · Mar 22, 2015 · Viewed 9.3k times · Source

Is it possible to put UISearchBar of UISearchController somewhere other than header view of UITableView?

In the apple's sample code for UISearchController, following is used.

[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar; 

Is it possible to position searchBar somewhere else? Say we want to implement a fixed UISearchBar like the one used in contacts app. I've tried this but the searchBar doesn't appear at all.

Answer

Praveen Gowda I V picture Praveen Gowda I V · Mar 22, 2015

You can place the UISearchBar of UISearchController in the navigation bar so that it remains fixed

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;