If you're just looking for some code and an explanation to go off of, I'd recommend looking at Apple's sample project here:
https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html
The main things to note are that you can't do this from a storyboard. Instead, you will create a property of type UISearchController
on the view controller where your search currently lives. Then, you'll create a new class that is either a subclass of UITableViewController
or has a UITableView
on it, and you will set that as the searchResultsController
of the your SearchController
property.
Another important thing to note is that unlike in the past with UISearchDisplayController
, UISearchController
is a whole different view controller that displays your search results on top of your main table view. (This is achieved by the definesPresentationContext
variable in the code).
While Apple provides you everything you need in the link above, here is the most important part of the code with some comments:
- (void)viewDidLoad {
[super viewDidLoad];
// results table controller is the same as what your search display controller used to be
_resultsTableController = [[APLResultsTableController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
// this says 'tell this view controller when search updates are available'
self.searchController.searchResultsUpdater = self;
// this places the search bar atop the table view since it's hard to do this via storyboard
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.
//
self.definesPresentationContext = YES; // know where you want UISearchController to be displayed
}