Swift: searchBar - how to change "Cancel" Button title

16,049

Solution 1

func searchDisplayControllerWillBeginSearch(controller: UISearchDisplayController) {
    self.searchDisplayController?.searchBar.showsCancelButton = true
    var cancelButton: UIButton
    var topView: UIView = self.searchDisplayController?.searchBar.subviews[0] as UIView
    for subView in topView.subviews {
        if subView.isKindOfClass(NSClassFromString("UINavigationButton")) {
            cancelButton = subView as UIButton
            cancelButton.setTitle("Vazgeç", forState: UIControlState.Normal)
        }
    }
}

Solution 2

Try accessing the button text using setValue, like this:

Swift 4

searchController.searchBar.setValue("New Title", forKey: "cancelButtonText")

it works for me :)

Solution 3

Put this in the viewDidLoad() of the view controller after initialising your search controller.

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "your button title"

Solution 4

For Swift 5

searchBar.delegate = self enter image description here

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
     let cBtn = searchBar.value(forKey: "cancelButton") as! UIButton
     cBtn.setTitle("MyCustomText", for: .normal)
}

Solution 5

Check this answer.

Example from the answer above updated for Swift 5:

if let cancelButton = searchController.searchBar.value(forKey: "cancelButton") as? UIButton {
    cancelButton.setTitle(<your_string>, for: <UIControlState>)
    cancelButton.setTitleColor(<your_uicolor>, for: <UIControlState>)
    cancelButton.setAttributedTitle(<your_nsattributedstring>, for: <UIControlState>)
}
Share:
16,049
user3742622
Author by

user3742622

Updated on June 22, 2022

Comments

  • user3742622
    user3742622 almost 2 years

    I have UIViewController which adopted UISearchBarDelegate. And set its delegate to self with: resultSearchController.searchBar.delegate = self. It works fine I tested it with searchBarCancelButtonClicked method%

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
            println("Сancel button tapped")
        }
    

    I see "Сancel button tapped" in console. But I would like to change "Cancel" Button title and I don't know how. I tried with:

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {      
            var barButton = UIBarButtonItem(title: "Button Title", style: UIBarButtonItemStyle.Done, target: self, action: "here")
            self.resultSearchController.searchBar.showsCancelButton = false
            self.resultSearchController.navigationItem.rightBarButtonItem = barButton
        }
    

    But it doesn't work. Could you help me please?

  • user3742622
    user3742622 about 9 years
    I tried your code. Nothing changed - I see search bar with "Cancel", sorry maybe I missed the point... Could you give me more information, please.
  • user3742622
    user3742622 about 9 years
    I don't use UISearchDisplayController it's deprecated. I use UISearchController. So this code cannot help me.
  • user3742622
    user3742622 about 9 years
    Please, I've already broke my brain in this hours... I don't use UISearchDisplayController it's deprecated. I use UISearchController.
  • SdaliM
    SdaliM over 5 years
    This worked in Swift 4, thanks! And it's way easier than the accepted answer
  • ekkeee
    ekkeee about 3 years
    Your answer helped me! But how can I address the same to bookmarkButton?