Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss keyboard when pressing Search key on tableView in swift 3?

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)

}
like image 393
QQ10 Avatar asked Dec 05 '22 15:12

QQ10


1 Answers

If you are using UISearchBar then first make sure its delegate is set and then add this function

func searchBarSearchButtonClicked(_ searchBar: UISearchBar)  {
    searchBar.resignFirstResponder()
}
like image 77
Ram Avatar answered May 14 '23 09:05

Ram