I have a search bar with cancel button. But when I click on Cancel button it doesn\'t close the search bar. How can I make that on click on Cancel it will return search bar to t
Try. It works fine.
class MyViewController: UIViewController, UISearchBarDelegate {
func searchBarTextDidBeginEditing(_searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
//write other necessary statements
}
func searchBarCancelButtonClicked(_searchBar: UISearchBar) {
searchBar.text = nil
searchBar.setShowsCancelButton(false, animated: true)
// Remove focus from the search bar.
searchBar.endEditing(true)
}
}