I have a search bar with cancel button. But when I click on Cancel button it doesn\'t close the search bar. How can I make that on click on Cancel it will return search bar to t
Very quick and simple. Just conform to the UISearchBarDelegate
protocol, and then implement this function in your class:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
resign
means that it will quit
FirstResponder
is the focus of the view
In conclusion, this takes focus away from the search bar, effectively canceling it.
I am also come across with this problem, in my case after searching any item from table view than click one of the result item it's opened details view but search bar does not disappeared. I use Nicat's answer with a little changes, here is my version:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
searchController.isActive = false
self.searchBarCancelButtonClicked(searchController.searchBar)
...
}
When you tap the cancel button, the searchBar
sees it like you've got a new thing to search, that's why if you put resignFirstResponder()
on the cancelButton
clicked it won't work, because after this it will call didBeginText
.
So I put a condition in didBeginText
, so when the string in the search has less or 0 characters it will resign the first responder. Just like this:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.characters.count < 1 {
searchBar.resignFirstResponder()
}
}
It's an easy approach and works as well. Best regards!
For Objective-C solution
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
if(@available(iOS 11.0, *)){
[self.navigationController.navigationItem.searchController.searchBar resignFirstResponder];
}
// reload your table or whatever you want.
// searchController is available after the iOS 11, so it will be good to use @available check
}
class MyController: UIViewController, UISearchBarDelegate {
// Called when search bar obtains focus. I.e., user taps
// on the search bar to enter text.
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchBar.text = nil
searchBar.showsCancelButton = false
// Remove focus from the search bar.
searchBar.endEditing(true)
// Perform any necessary work. E.g., repopulating a table view
// if the search bar performs filtering.
}
}
You need to implement the UISearchBarDelegate :
class ViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
Set the search bar delegate to self
override func viewDidLoad() {
super.viewDidLoad()
searchBar.showsCancelButton = true
searchBar.delegate = self
and then implement the butCancelSearchAction delegate function to do whatever you need to do to cancel the search action and reset the search text:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
// Do some search stuff
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
// Stop doing the search stuff
// and clear the text in the search bar
searchBar.text = ""
// Hide the cancel button
searchBar.showsCancelButton = false
// You could also change the position, frame etc of the searchBar
}