1

I want to just hide keyboard when user first press cancel button.
Just likes AppStore.app

I use UISearchController like that:

    navigationItem.searchController = searchController

============== Update ==============
This Cancel button is hosted by UISearchController.

user25917
  • 727
  • 5
  • 16
  • Possible duplicate of [Dismissing the keyboard when a button is pressed, programmatically with swift?](https://stackoverflow.com/questions/31952064/dismissing-the-keyboard-when-a-button-is-pressed-programmatically-with-swift) – DvixExtract Nov 01 '18 at 06:09

1 Answers1

1

If you did search bar programmatically, you can add extension on your ViewController or else just use function call as below.

 extension ViewController: UISearchBarDelegate {
     func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
         if let text = searchBar.text {
             self.filterContent(text: text)
             searchBar.resignFirstResponder()
         }
     }
 }
Adam Bardon
  • 3,609
  • 7
  • 37
  • 70
AzeTech
  • 426
  • 8
  • 15