The Hit List Diary #12 – UISearchController

I’ve recently gotten the chance to play with iOS 8’s brand new UISearchController. On the whole I have to say I like it. The setup is quite a bit cleaner than the old UISearchDisplayController.

For me, the couple of paragraphs of introduction at the top of the docs didn’t really seem enough to get started with. Fortunately, Apple’s sample code is rather illuminating.

An important lesson learned: You must turn on .definesPresentationContext for the view controller being searched from. Without it, the presentation will be rather odd — the search area not quite reaching the top of the screen for example — and you’ll have little to go on.

UISearchController seems to cope poorly with a search bar in the “minimal” style (for that matter, UISearchBar in its non-active state often seems to struggle with itself!). I found the background covering for most of the search bar was missing, which is very disconcerting as search results scroll under the bar and then appear above it! The best solution I have is to implement UISearchControllerDelegate such that it switches the search bar back to regular style during searching.

The biggest thing I miss from UISearchDisplayController is ease of setup in Interface Builder. Out of the box, UISearchController provides and owns the search bar it uses. So it’s easiest to create the search controller programatically, and configure the search bar as part of that. If I wanted to configure the bar in IB, some possibilities:

  • Copy the IB-configured bar’s properties over to the UISearchController-supplied one and then swap them
  • Apple could extend UISearchController’s API to have an initialiser method which allows me to pass in my own pre-existing search bar (rdar://18885156)
  • The docs do say "To create a custom UISearchBar, subclass the UISearchController class and return your custom object”, so I suppose I could create my own subclass that implements my desired API


Oh, and there’s a bug. The docs claim -updateSearchResultsForSearchController: will be called whenever the search text or scope changes. In reality I’m finding the latter isn’t true. Fortunately it’s pretty easy to work around by becoming the search bar’s delegate, and using that to trigger your own update of search results.

© Mike Abdullah 2007-2015