Search Bar in iOS Applications

In iOS applications, we often need to filter data from a list using some keyword. For this purpose, Apple provides UISearchBar for users to search through a collection of items in the iOS app. It is a specialized view used to receive search-related information from the user, such as keywords.

In this article, we will discuss how we can use the search bar in iOS applications to implement search functionality for users. However, the search bar in iOS provides only the user interface; we need to implement any filtering logic according to our requirements.

Using UISearchBar

It is the most used way to implement search functionality in the iOS app. It is flexible to use UISearchBar in the iOS app if we have various other items on a screen like collection view, table view, or bar button items.

In simple words, the UISearchBar provides a text field for entering the search text, a search button as the right view of the text field, a bookmark button, and a cancel button. We need to conform to the UISearchBarDelegate protocol and implement the UISearchBar delegate methods to perform actions when the search text is entered in the search bar or buttons are clicked.

In swift, we use the UISearchBar class object to implement the search bar. The syntax of UISearchBar is given below.

The UISearchBar class includes the following properties to change the appearance inducing the event trigger delegates.

SNPropertyDescription
1var delegate: UISearchBarDelegate?It represents the delegate object for UISearchBar. The view controller needs to conform to the UISearchBarDelegate protocol and implement its delegate methods.
2var placeholder: String?It represents the placeholder text shown in the search text field.
3var prompt: String?It represents a single line of text displayed at the top of the search bar.
4var text: String?It represents the search text used to search across the data list shown either in collectionview or tableview.
5var barTintColor: UIColor?It represents the tint color to be applied to the search textfield background.
6var searchBarStyle: UISearchBar.StyleIt represents the search bar style assigned among the values of UISearchBar.Style Enum.
7enum UISearchBar.StyleIt is an Enum type variable that represents whether the search bar has a background or not.
8var tintColor: UIColor!It represents the tint color to be applied to the key elements in the search bar.
9var isTranslucent: BoolIt is a Boolean type property that is used to determine whether the search bar will be translucent or not.
10var barStyle: UIBarStyleIt is an instance of enum type UIBarStyle, which specifies the search bar appearance.
11enum UIBarStyleIt represents the stylistic appearance of different types of views.
12var inputAssistantItem: UITextInputAssistantItemIt is the input assistant item that is used to configure the keyboard's shortcut bar.
13var showsBookmarkButton: BoolIt is a Boolean type property that represents whether the bookmark button is shown in the search bar or not.
14var showsCancelButton: BoolIt is a Boolean type property that represents whether the cancel button is shown in the search bar or not.
15var showsSearchResultsButton: BoolIt is a Boolean type property that represents whether the search result button is shown in the search bar or not.
16var isSearchResultsButtonSelected: BoolIt is a Boolean type property that represents whether the search results button has been selected in the search bar or not.
17var scopeButtonTitles: [String]?It is an array of strings that specifies the titles of the scope button.
18var selectedScopeButtonIndex: IntIt represents the index of the selected scope button.
19var showsScopeBar: BoolIt is a Boolean type property that represents whether the scope bar is shown in the search bar or not.
20var backgroundImage: UIImage?It is the background image shown in the UISearchBar.

The most important delegate methods used to trigger the user actions are given below.

SNMethodDescription
1func searchBar(UISearchBar, textDidChange: String)This method notifies that the user changed the search bar text.
2func searchBarShouldBeginEditing(UISearchBar) -> BoolIt asks the delegate if the text in the specified range should be replaced with the given text.
3func searchBarTextDidBeginEditing(UISearchBar)This method notifies the delegate that the user begins editing the search text.
4func searchBarShouldEndEditing(UISearchBar) -> BoolIt asks the delegate that editing should stop in the specified search bar.
5func searchBarTextDidEndEditing(UISearchBar)It notifies the delegate that the user has done editing the text in the search bar.

Example

Let's create the XCode project in which we will filter the data in tableview. For this purpose, create a project in XCode as SearchBarExample. Let's create the interface for the project in the Main.storyboard.

Interface builder

In the storyboard, first, add a search bar which will be at the top of the parent view. To add a search bar, start typing or browse for the search bar in the objects library, as shown below.

Search Bar in iOS Applications

Now, align the search bar to the top of the view with leading and trailing assigned to 0 with the Super View. Now, add a tableView to the ViewController, which contains the list of data in the project.

Search Bar in iOS Applications

Now, create the outlets for the search bar and table view in ViewController class.

ViewController.swift

As we are using UISearchBar and UITableView in this application, we need to conform our view controller to UISearchBarDelegate, UITableViewDelegate UISearchBarDataSource protocols. The ViewController has the following code to implement the search functionality in the project.


Next TopicUIImage in iOS




Latest Courses