iOS8 Cannot hide cancel button on search bar in UISearchController iOS8 Cannot hide cancel button on search bar in UISearchController ios ios

iOS8 Cannot hide cancel button on search bar in UISearchController


I think there are three ways of achieving that:

  1. Override searchDisplayControllerDidBeginSearch and use the following code:

searchController.searchBar.showsCancelButton = false

  1. Subclass UISearchBar and override the layoutSubviews to change that var when the system attempts to draw it.

  2. Register for keyboard notification UIKeyboardWillShowNotification and apply the code in point 1.

Of course can always implement your search bar.


For iOS 8, and UISearchController, use this delegate method from UISearchControllerDelegate:

func didPresentSearchController(searchController: UISearchController) {  searchController.searchBar.showsCancelButton = false}

Don't forget to set yourself as the delegate: searchController.delegate = self


Simply subclass UISearchController & UISearchBar.

class NoCancelButtonSearchController: UISearchController {    let noCancelButtonSearchBar = NoCancelButtonSearchBar()    override var searchBar: UISearchBar { return noCancelButtonSearchBar }}class NoCancelButtonSearchBar: UISearchBar {    override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) { /* void */ }}