0

Pressing the search button on the keyboard and then pressing the cancel button doesn’t always dismiss the cancel button even when showCancelButton is equal to false or when tagging IOS 13 for automatic cancel button dismissal. This works in simulator but only 50% of the time on my device. Anyone know of any possible solutions?

func setupSearchController() {
    if #available(iOS 13.0, *) {
        searchController.automaticallyShowsCancelButton = true
    } else {
        searchController.searchBar.showCancelButton = false
    }
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    if #available(iOS 13.0, *) {
        searchController.automaticallyShowsCancelButton = true
    } else {
        searchController.searchBar.showCancelButton = false
    }
}

func updateSearchResults(for searchController: UISearchController) {
    if #available(iOS 13.0, *) {
        searchController.automaticallyShowsCancelButton = true
    } else {
        searchBar.showsCancelButton = true
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Joshua Hart
  • 772
  • 1
  • 21
  • 31
  • Perhaps related to https://stackoverflow.com/questions/58134631/ios-13-uibarbuttonitem-not-clickable-and-overlapping-uinavigationbars-when-using – matt Oct 21 '19 at 23:01
  • Maybe this can help you, https://stackoverflow.com/questions/58468949/is-there-a-way-to-only-activate-the-cancel-button-on-a-search-bar-while-editing . Try removing your cancel button code. – NicolasElPapu Oct 22 '19 at 02:01

1 Answers1

0

If I have understood the problematic correctly, it seems as it's a question of bad timing. In this case the solution would be to make a short delay:

func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
    if #available(iOS 13.0, *)
    {
        let deadlineTime = DispatchTime.now() + .seconds(0.3)
        DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
            // your code here, such as
            searchController.automaticallyShowsCancelButton = true
        }
    }
    else
    {
        searchController.searchBar.showCancelButton = false
    }
}