Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a UISearchController to a Non-Table View

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.

like image 608
pbuchheit Avatar asked Oct 10 '14 15:10

pbuchheit


1 Answers

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.

like image 139
Praveen Gowda I V Avatar answered Oct 03 '22 03:10

Praveen Gowda I V