I have presented a screen using navigation controller and in that screen I have a search bar , which I have made as first responder in viewWillAppear() . The problem is that I want to hide keyboard when done button is clicked or cancel in searchBar is clicked. But on doing the same with both resignFirstResponder() and searchBar.endEditing(true) , it also hides UISearchBar. I want to show UISearchBar when the state is not in edit also .
Basically what I have done is I have made my UISearchBar my first responder as such :
override func viewWillAppear(_ animated: Bool) {
searchBar.becomeFirstResponder()
}
then when user clicks search I have made :
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
// Remove focus from the search bar.
}
Same is the case with cancel button . But in my case instead of just dismissing the keyboard , this also hides UISearchbar() after calling the above function.