6
votes

I'm having trouble with using the new UISearchController in IOS8. Every example I've found so far uses the search bar as the header view of a UITableView. What do you do when the search bar needs to be displayed somewhere else? For example, placing the searchbar outside the table to prevent it from scrolling? What about using it with something like a UICollectionView?

Am I missing something here? It doesn't seem like this should be that complicated.

1

1 Answers

3
votes

Placing the UISearchBar in the titleView of the navigation bar, you can prevent it from scrolling when the tableview is scrolled.

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;

UISearchController relies on searchResultsController which should be the view controller that manages the results of the search. Make sure that the view controller pointed to by searchResultsController property updates based on the text in UISearchBar when the UISearchController is active. This way you can use the UISearchController with tableview or collectionview.