Hiding Cancel button on search bar in UISearchController
Asked Answered
B

6

9

I'm trying to hide the Cancel button of the search bar in the UISearchController, but unfortunately setting the following in viewDidLoad() does not work:

override func viewDidLoad() {
    super.viewDidLoad()

    searchResultsTableController = UITableViewController()
    searchResultsTableController.tableView.delegate = self

    searchController = UISearchController(searchResultsController: searchResultsTableController)
    searchController.searchResultsUpdater = self
    searchController.searchBar.sizeToFit()
    searchResultsView.tableHeaderView = searchController.searchBar

    searchController.delegate = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.delegate = self

    searchController.searchBar.searchBarStyle = .Minimal
    searchController.searchBar.showsCancelButton = false

    definesPresentationContext = true
}

I have also tried using the above code in this delegate method:

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

This approach works but will show the Cancel button briefly before hiding it, which is not ideal. Any suggestions?

Boaz answered 20/10, 2015 at 2:52 Comment(1)
can you elaborate more on your implementation? where did you put searchController.searchBar.showsCancelButton = falseHoenack
B
28

I ended up subclassing both UISearchBar and UISearchController as suggested:

CustomSearchBar.swift

import UIKit

class CustomSearchBar: UISearchBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        setShowsCancelButton(false, animated: false)
    }
}

CustomSearchController.swift

import UIKit

class CustomSearchController: UISearchController, UISearchBarDelegate {

    lazy var _searchBar: CustomSearchBar = {
        [unowned self] in
        let result = CustomSearchBar(frame: CGRectZero)
        result.delegate = self

        return result
    }()

    override var searchBar: UISearchBar {
        get {
            return _searchBar
        }
    }
}
Boaz answered 21/10, 2015 at 15:37 Comment(3)
Thank you for posting this solution. I'm going to file a radar for this, as one would expect setShowsCancelButton would stick throughout the object's lifecycle using the factory object.Jobye
Filed radar # 29859105Jobye
I'm on Swift 3 & iOS 10 and this almost works. Instead, I tried other answer from this thread and got a result. Instead of overriding layoutSubviews, you override setShowsCancelButton(:animated:).Nel
S
7

Rejoice! As of iOS 13, there is access to automaticallyShowsCancelButton on UISearchController. Set it to false to hide the cancel button.

Sletten answered 30/9, 2019 at 17:31 Comment(1)
for me the cancel button flies across the screen and then hides.Emogene
R
3
func didPresentSearchController(_ searchController: UISearchController) {
    searchController.searchBar.becomeFirstResponder()
    searchController.searchBar.showsCancelButton = true
}


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

In my case all the above solutions dint work. You need to show in didPresentSearchController and hide it in didDismissSearchController. Earlier I was just hiding in didDismissSearchController which was still showing Cancel button on cancelling.

Roundelay answered 20/11, 2019 at 11:20 Comment(0)
A
1

Hide the Cancel button in search bar delegate methods and set your delegate searchController.searchBar.delegate=self UISearchBarDelegate

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {

}
Armington answered 20/10, 2015 at 3:43 Comment(2)
Unfortunately this doesn't work. The Cancel button still shows up unaffected.Boaz
have u set the delegate properly,check whether delegate func are getting called or not?Its working on ma endArmington
O
1

Try subclassing UISearchBar and implement:

override func layoutSubviews() {
   super.layoutSubviews()
   self.setShowsCancelButton(false, animated: false)
}

This SO thread may help you more in this direction.

Octodecimo answered 20/10, 2015 at 3:55 Comment(0)
S
0

The same answer as given by @Griffith and @Abhinav but using extension:

extension UISearchBar {
    override open func layoutSubviews() {
        super.layoutSubviews()
        setShowsCancelButton(false, animated: false)
    }
}

This code is from Swift 4.

Sonatina answered 22/3, 2018 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.