Cancel Button Becomes Inactive

My screen does not have a navigation bar and I am using the cancel button to dismiss the view. This works if the search bar isEnabled.
But the cancel button is not enabled after the search button is clicked.

I noticed the demo and examples don’t have to call the searchBarSearchButtonClicked function but I did for some reason in order to dismiss the keyboard.

// MARK: - SearchBarDelegateProtocol
public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    self.dismiss(animated: true, completion: nil)
}

public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchViewModel.search(query: searchText)
}

public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchViewModel.search(query: searchBar.text)
    self.collectionViewHeightConstraint.constant = 0
}

public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
   self.collectionViewHeightConstraint.constant = 175
}

public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).isEnabled = true
}

Hey @phil.manning11,

Can you send me a screencast/gif in order to visualize the issue and what is going on? :slight_smile:

The screenshot with the banner is without the keyboard active and when the search is active only then is the dismiss button enabled…

00 PM43 PM

I solved this by adding this line to the viewDidLoad
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).isEnabled = true

Strange that it needed it though as I noticed this was the way the search bar worked in the demos. I think it may be due to the fact that I’m using the search bar delegate.