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
What about setting it with
[searchBar setShowsCancelButton:NO animated:NO];
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/occ/instm/UISearchBar/setShowsCancelButton:animated:
Easy solution in Swift3 - we need to make CustomSearchBar without cancel button and then override the corresponding property in new CustomSearchController:
In MyViewController I initialize and configure searchController using this new custom subclass:
This worked for me (iOS 10):
I try to help you man but I'm not sure that I find the real problem.
According to Apple Documentation:
But for hide the cancel button maybe you should use:
I hope that can be helpful.
We wanted the search bar to have no Cancel button initially, but have it appear when the user tapped in the search bar. Then we wanted the Cancel button to disappear if user tapped Cancel, or otherwise the search bar lost first responder.
What finally worked for me:
On create:
We use a subclass of UISearchBar and override searchBarShouldBeginEditing thusly:
We also override resignFirstReponder (in the UISearchBar subclass) thusly:
You can subclass UISearchBar and override method
layoutSubviews