I am looking to implement a search such that when the user presses a search result, the relevant UIViewController
is pushed onto the search result itself, allowing the user to press back to view the search result again.
I have tried simply pushing the ViewController onto the presenting ViewController's UINavigationController
. This results in the UIViewController
appearing behind the search result (it can be seen when canceling the search result).
I have also tried subclassing UINavigationController
and making it conform to UISearchResultsUpdating
, proxying the updates to the UIViewController
. This results in almost the correct behavior, but the search bar remains with the navigation bar when a view controller is being pushed onto the UINavigationController
, as can be seen in the image below.
I could simply do some hacky code to hide/show the search bar depending on the state of the UINavigationController
, but it seems like there must exist a cleaner way of accomplishing this.
Apple has themselves done this in their Notes app, if you do a search and press a result, you are brought to the relevant note, pressing back takes you back to the search result. So I am wondering how to replicate this behavior correctly.
I later solved this the hacky way by making the
UIViewController
that is responsible for displaying the search result conform to theUINavigationControllerDelegate
protocol (in addition toUISearchResultsUpdating
). TheupdateSearchResultsForSearchController(searchController: UISearchController)
method then catches and stores a reference to theUISearchController
that is passed as a parameter. Then this juggling with view-logic in theUINavigationControllerDelegate
code makes everything work and look as expected:Lastly, as explained in the third paragraph above, I made a subclass of
UINavigationController
which is assigned as the search results controller, and which delegates navigation events andUISearchResultsUpdating
events to theUIViewController
.This certainly works as intended and I not experienced any bugs with this so far. Will post an update if I find a cleaner solution next time I look over this code (might be easier in iOS 9).