Search Bar and UISearchController
I knew that if I failed I wouldn't regret that, but I knew the one thing I might regret is not trying.
– Jeff Bezos
For most of the table-based apps, it is common to have a search bar at the top of the screen. How can you implement a search bar for data searching? In this chapter, we will add a search bar to the FoodPin app. With the search bar, we will enhance the app to let users search through the available restaurants.
Since the release of iOS 8, a new class called
UISearchController was introduced to replace the
UISearchDisplayController API that has been around since iOS 3. The old API is now deprecated. If you have some experience with iOS 7 or older version of SDK, remember to use
UISearchController API simplifies the way to create a search bar and manage search results. You're no longer limited to embed search in table view controller but can use it in any view controller like collection view controller. Even more, it offers developers the flexibility to influence the search bar animation through a custom animator object.
In iOS 11, Apple further simplified the implementation of the search bar. It introduces a new
searchController property on navigationItem of the navigation bar. With just a few lines of code, you will be able to add a search bar to the navigation bar. You will understand what I mean in a while.
UISearchController, adding a search bar to your app is quite an easy task. Let's get started to implement a default search bar and see how we can filter the restaurant data.
To continue reading and access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.