Add Search Bar Quickly

Quang Quoc Tran
2 min readFeb 25, 2019

--

The fast way to add a search bar into your view controller is to use UISearchController. UISearchController provide you a search bar and notify you when user enter something into the search bar.

In addition, UISearchController also responsible for show or dismiss the results view that you provide for it at the initialization.

To get started, you download the starter project. The starter project has already created for you a table view controller that show the flowers list and another table view controller that show filtered flower list when user enter search string into the search bar.

Run the starter project you will see the flower list, and you will not see any search bar at anywhere.

Initialize Search Controller

Now we will initialize search controller and add it into navigation item. Add the following block code into the end of viewDidLoad method in MainTableViewController.swift file.

Update the Search Results

When user enter something into the search bar then the search controller will notify you thru updateSearchResults(for:) method. From this method you can get the search string in the search bar, filter the flowers and update them on results table view.

Add the following block code into MainTableViewController.swift file and run the app.

Now you could see the search bar and can filter them. You can download the final source code project here.

Please Support Me!

If this post is useful, please support me by trying my free Unimal app. Thanks for your support.

--

--