1

I'm having a weird issue where when I click the "x" in the search bar to clear the text field, it registers as a gesture on the Cancel/Done button and dismisses the view (see gif in link below). But when I click a few points to the left of the clear button, it performs the desired action of clearing the text field. Below is my code to configure the search bar table view

https://giphy.com/gifs/oXA94eUOyaVTW

func presentSearchBarViews() {
    searchController.searchBar.sizeToFit()

    tableView.tableHeaderView = searchController.searchBar
    tableView.tableFooterView = UIView()
    if #available(iOS 9.0, *) {
    searchController.searchBar.setValue("Done", forKey:"_cancelButtonText")

    searchController.searchBar.placeholder = "Search for food"
    searchController.searchBar.showsCancelButton = true
    searchController.searchBar.layer.borderWidth = 1;
    searchController.searchBar.layer.borderColor = UIColor.init(red: 0.45, green: 0.78, blue: 0.77, alpha: 1.0).CGColor
    searchController.searchBar.delegate = self
}

I'm pretty stumped on how to solve this one since I'm configuring the search bar programmatically. Any help is much appreciated!

UPDATE

Setting searchController.searchBar.showsCancelButton = false doesn't seem to get rid of the Done/Cancel button like it should. What could possible be the cause of this?

Obi Anachebe
  • 123
  • 1
  • 13
  • What happens when you comment out `searchController.searchBar.delegate = self`? – iwatakeshi Sep 11 '16 at 22:32
  • @iwatakeshi still the same issue except the the gesture that is recognized for the "Done" button doesn't clear the view. It still assigns the clear button gesture to the done button gesture mistakenly like it did before – Obi Anachebe Sep 11 '16 at 22:38
  • Hmm, so I think we've narrowed it down to something before that line. I might be wrong but it could be `tableView.tableHeaderView = searchController.searchBar` or the line below that. – iwatakeshi Sep 11 '16 at 22:42
  • @iwatakeshi I actually think we need this line in order to place the search bar view to the tableview headerview. Are you able to reproduce the issue at all? – Obi Anachebe Sep 11 '16 at 22:48
  • No, sorry. Have you looked at this [question](http://stackoverflow.com/questions/24030348/how-to-create-a-button-programmatically)? – iwatakeshi Sep 11 '16 at 22:53
  • @Obi Anachebe I’m facing the same issue now, did you find a solution for this? – Eman Shedeed Mar 05 '22 at 07:12
  • @EmanShedeed I ended up just using a `UISearchBar` with a custom close button. The `UISearchBarDelegate` has a textDidChange method you can implement to update your data source as the input value changes – Obi Anachebe Mar 09 '22 at 18:11

0 Answers0