UISearchController lets users enter search keywords on the UISearchBar, display search results or perform other operations. UISearchController connects two controllers (UIViewController) together. The parent controller places the UISearchBar control. When the user clicks on the search box, the UISearchBar will move to the top of the screen; enter the search keyword, and the view of the subcontroller appears below the UISearchBar. Of course, you can also use code to display the UISearchBar and child controllers, even if the parent controller does not place the UISearchBar.
General usage
- Create a child controller in the parent controller
- Create a UISearchController with a child controller, which usually turns the UISearchController into a property of the parent controller.
- Set the searchResultsUpdater property of UISearchController
- Usually the searchBar of the UISearchController is placed on the parent controller
- Set the parent controller definitionsPresentationContext property to true
The following is a parent controller code example
let searchResultsVC = SearchResultsVC(allStrings: allStrings) searchController = UISearchController(searchResultsController: searchResultsVC) = searchResultsVC; let tableView = UITableView(frame: ) = self (tableView) = ; = UIView() definesPresentationContext = true;
The searchResultsUpdater property of UISearchController is generally set to the parent controller or child controller. The value of this property must comply with the UISearchResultsUpdating protocol, and implement the updateSearchResults(for searchController: UISearchController) method. This method will be called when the searchBar becomes the first responder (for example, when the user clicks on the search box, the keyboard pops up), and when the search keyword changes. Write code to perform search and update UI in this method.
The above code uses the subcontroller as the searchResultsUpdater of the UISearchController. Suppose the data source of the subcontroller is allStrings and strings, both of which are Arrays containing Strings. Where allStrings is a constant, containing all Strings; strings contain Strings that meet the search criteria and need to be updated when user inputs. The search criteria are, which contain content entered by the user and are case-insensitive. Use UITableView to display search results. In the updateSearchResults(for searchController: UISearchController) method, get the text of the searchBar, update the strings, and update the UI.
The following is a subcontroller code example
func updateSearchResults(for searchController: UISearchController) { () if let text = ?.uppercased(), ! { strings = { $(text) } } () }
Change style
By default, the user clicks on the search box, hides the navigation bar, moves UISearchBar up, and you can see the parent controller below, but it has a gray mask block. Click on the gray mask to return to the parent controller. If the hidesNavigationBarDuringPresentation property of UISearchController is set to false, the navigation bar will not be hidden. If the dimsBackgroundDuringPresentation property 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 subcontroller is hidden
If the searchBar of the 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. At this time, the updateSearchResults(for searchController: UISearchController) method will be called, but the view of the sub-control does not appear, and isHidden is true. The view of the subcontroller appears after entering the content. Clear the input content and the view of the subcontroller disappears. If you want to display the subcontroller when the search box is empty, add ?. = false to the updateSearchResults(for searchController: UISearchController) method.
Code displays UISearchBar and subcontroller methods
In parent control, you can use code to display the UISearchBar and child controller. The specific implementation method depends on whether the searchBar of the UISearchController is placed on the parent controller.
If the searchBar of UISearchController is placed on the parent controller
UISearchBar moves upwards and pops up the keyboard (the same effect as the user clicks on the search box)
()
UISearchBar moves up, but does not pop up the keyboard
present(searchController, animated: true, completion: nil)
or
= true
If the searchBar of UISearchController is not on the parent controller
UISearchBar appears from the top and the keyboard pops up
present(searchController, animated: true, completion: nil)
Once the search box comes out, it becomes the first responder, and the keyboard will pop up. I don’t know how to prohibit the keyboard from popping up. Of course, it is a strange requirement to display the search box without the keyboard popping up.
The code has been uploaded to GitHub:/Silence-GitHub/SearchControllerDemo
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!