2

What I need is that after taping on Search button, the searchBar returns to its initial state, so I can get to any UI element in one tap. Because currently I need to tap anywhere on screen once before I have ability to interact with other UI elements. I've tried searchBar.resignFirstResponder() and searchBar.endEditing(true) and other UIView.endEditing, but it won't work. Thanks for advance!

For example,

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    //search products, reload table and then:

    searchBar.resignFirstResponder()
}

UPDATE: Found the solution! searchController.isActive = false

matt
  • 515,959
  • 87
  • 875
  • 1,141
neuro
  • 21
  • 4
  • 1
    Did you assigned `searchBar.delegate` to `self` in view controller? – Peter Tretyakov Oct 16 '18 at 20:31
  • Yeap, all delegate methods work fine. – neuro Oct 16 '18 at 20:43
  • `searchBar.resignFirstResponder()` _should_ work perfectly fine. Just make sure you are conforming to the `UISearchControllerDelegate` protocol. – George Oct 16 '18 at 20:56
  • I've added UISearchControllerDelegate, but it doesn't help. searchBar.resignFirstResponder() still not working. I've updated my post with solution thats works for me. Thx! – neuro Oct 16 '18 at 21:20

1 Answers1

0

If I understand you correctly, you need to hide Cancel button after you clicked it. If yes please try this.

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    searchBar.showsCancelButton = true
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchBar.text = nil
    searchBar.showsCancelButton = false
    searchBar.endEditing(true)
}
Pavlo Boiko
  • 583
  • 5
  • 13
  • 1
    No, I don't need to hide it, I need to "tap" it programmatically, e.i. I need programmatically to cancel search. I've updated my post with solution already, but thank you for your answer) – neuro Oct 16 '18 at 21:15