How to use iOS UISearchController

  • 2021-10-27 09:23:24
  • OfStack

UISearchController lets users enter search keywords, display search results, or do other things on UISearchBar. UISearchController connects two controllers (UIViewController) to one. The parent controller places the UISearchBar control. When the user clicks on the search box, UISearchBar will move to the top of the screen; Enter the search keyword, and view of the sub-controller appears under UISearchBar. Of course, you can also use code to exhibit UISearchBar and child controllers, even if the parent controller does not place UISearchBar.

1 general usage

Create a child controller in a parent controller Creating an UISearchController with a child controller usually makes UISearchController an attribute of the parent controller Setting the searchResultsUpdater property of UISearchController searchBar of UISearchController is usually placed on the parent controller Set the parent controller definesPresentationContext property to true

The following is an example of the parent controller code


let searchResultsVC = SearchResultsVC(allStrings: allStrings)
searchController = UISearchController(searchResultsController: searchResultsVC)
searchController.searchResultsUpdater = searchResultsVC;
let tableView = UITableView(frame: view.bounds)
tableView.dataSource = self
view.addSubview(tableView)
tableView.tableHeaderView = searchController.searchBar;
tableView.tableFooterView = UIView()
definesPresentationContext = true;

searchResultsUpdater property 1 of UISearchController is generally set to parent controller or child controller. The value of this property should conform to the UISearchResultsUpdating protocol and implement the updateSearchResults (ES30searchController: UISearchController) method. This method is called when searchBar becomes the first responder (for example, when the user clicks the search box, the keyboard pops up) and the search keywords change. In this method, write the code that performs the search and updates UI.

The above code uses the child controller as the searchResultsUpdater of UISearchController. Assume that the data sources of the sub-controller are allStrings and strings, both of which are Array with String. Where allStrings is constant, including all String; The strings contains an String that matches the search criteria and needs to be updated when the user enters. The search condition is that it contains user input and is case-insensitive. Show search results with UITableView. In the updateSearchResults (for searchController: UISearchController) method, text of searchBar is obtained, strings is updated, and UI is updated.

The following is a sample child controller code


func updateSearchResults(for searchController: UISearchController) {
 strings.removeAll()
 if let text = searchController.searchBar.text?.uppercased(), !text.isEmpty {
  strings = allStrings.filter { $0.contains(text) }
 }
 tableView.reloadData()
}

Change the style

By default, the user clicks on the search box, the navigation bar (navigation bar) is hidden, UISearchBar moves up, and the parent controller can be seen below, but it is covered by a gray mask. Click on the gray mask and return to the parent controller. If the hidesNavigationBarDuringPresentation property of UISearchController is set to false, the navigation bar is not hidden. If the dimsBackgroundDuringPresentation attribute of UISearchController is set to false, the gray mask will not be displayed, and the parent controller can be clicked.

When the search box is empty, the child controller hides

If the searchBar of UISearchController is already placed on the parent controller, when the user clicks the search box, the UISearchBar will move up to the top of the screen and the keyboard will pop up. The updateSearchResults (for searchController: UISearchController) method is called, but the child control view does not appear, and isHidden is true. The view of the child controller appears after entering the content. Empty the input, and the view of the sub-controller disappears. Add searchController. searchResultsController to the updateSearchResults (for searchController: UISearchController) method if you want to display child controllers when the search box is empty? . view. isHidden = false will do.

The code shows the method of UISearchBar and sub-controller

In the parent control can use code to show UISearchBar and child controller, the specific implementation method depends on whether searchBar of UISearchController is placed on the parent controller.

If the searchBar of UISearchController is placed on the parent controller

UISearchBar moves up and pops up the keyboard (like the effect of the user clicking on the search box 1)

searchController.searchBar.becomeFirstResponder()

UISearchBar moves up, but does not eject the keyboard

present(searchController, animated: true, completion: nil)

Or

searchController.isActive = true

If searchBar of UISearchController is not on the parent controller

UISearchBar appears from the top and pops up the keyboard

present(searchController, animated: true, completion: nil)

Search box 1 to become the first responder, the keyboard will pop up, do not know how to prohibit the keyboard pop-up. Of course, showing the search box without popping up the keyboard is a strange requirement.

Code uploaded to GitHub: https://github.com/Silence-GitHub/SearchControllerDemo


Related articles: