How to dismiss keyboard when pressing Search key? I have a search bar on tableView and I used the following method. It did't work. Any Idea why? Thanks in advance.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
UPDATE 2.0 ----------------------------------------------------
This picture is what I am trying to do, when I click search key nothing happened. enter image description here
I have my UISearchBar delegate has set already
class SearchVC: UIViewController, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var data = [// my data here.]
var filteredData: [String]!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
searchBar.delegate = self
filteredData = data
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
tableView.addGestureRecognizer(tap)
}
If you are using UISearchBar then first make sure its delegate is set and then add this function
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With