I've added a UISearchController to my application and set it's searchBar to the titleView
of my navigationItem
.
This works but I am seeing the cancel button despite having set showsCancelButton
to false
.
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = searchResultsUpdater
// Configure the searchBar
searchController.searchBar.placeholder = "Find Friends..."
searchController.searchBar.sizeToFit()
searchController.searchBar.showsCancelButton = false
self.definesPresentationContext = true
navigationItem.titleView = searchController.searchBar
my solution was to set the attribute every time anew when I used the searchcontroller respectively its searchbar. I initialized the searchcontroller lazily without setting the attribute and then did
every time before search began. You could do this in the UISearchControllerDelegate methods i.e...
I had to correct by putting in a little hack...
Setting the alpha to 0.0 on viewDidLoad because he screen will flash.
Before you ask...willPresentSearchController will not work.
I agree, it seems like a bug. The problem is that the
searchController
keeps resetting theshowsCancelButton
property of the searchBar. I found a solution that involves:UISearchBar
to ignoresetShowsCancelButton
.UISearchController
.Convoluted, but it seems to do the trick. You can find the full answer here.
This appears to be a bug in iOS. The same behavior I've described can be seen in the example project supplied by Apple
https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html
The documentation states that the default for this is
NO
but this doesn't seem to be the case. SettingshowsCancelButton
toNO
seems to have no effect.I have filed a radar for this and am waiting to hear back.
It may be helpful to note that this has changed on iOS 13 and quote Apple's documentation on
showsCancelButton
, currently only available on UISearchBar.h and not on developer.apple.comautomaticallyShowsCancelButton
has been introduced on iOS 13.0 and should clarify what @pbasdf had already pointed out in his answer: that the buggy behavior is something intrinsic toUISearchController
.I would also add
See if assigning those delegates will help.