- iOS 13.2+
- Xcode 11.4+
This sample project demonstrates how to implement a
UISearch in your application.
UISearch is a view controller that manages the display of search results based on interactions with a search bar.
The sample lists flower products. Users can search for them by title, price, year, and color. As soon as the user taps the search bar, the search controller displays the list of suggested searches. A suggested search represents a particular color: red, blue, or green. The search controller filters the list of flower products by color when a suggested color is selected. This behavior matches that of the Mail app.
This sample also demonstrates a scene-based architecture using
UIScene. It includes the recommended
UIUser protocol to save and restore the search results. Adopt this protocol to save the search bar’s active state, first responder status, search bar text and token, and restore them when the app is relaunched.
Create a Search Controller
Main, a subclass of
UITable, creates the search controller. The search controller’s search bar helps filter a set of
Product objects and displays the results in the table view. The sample places the search controller in the
Main navigation bar:
Results displays both the list of filtered flower products and the list of suggested search colors. As the user types text in the search field, the list of flower products matches what is typed. If the user doesn’t enter any text, the search controller displays a list of suggested search colors. A suggested search represents a single query to search for a specific color of a product.
Create Suggested Searches
The list of suggested searches is displayed by the results controller once the search bar is tapped:
When the user first taps the search bar, the search results controller displays the suggested searches. The user chooses a suggested search, and then types additional search criteria in the search bar. Each suggested search represents a
UISearch, a visual representation of a search query. Tapping a suggested search creates a search token for a particular color and the search controller places it in the search bar’s text field. This text field is represented by
UISearch which supports cut, copy, paste, and drag and drop of search tokens. Tokens always precede the text and can be selected and deleted by the user.
The sample creates a
UISearch as follows:
This sample inserts the search token into the search bar’s search text field as follows: