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

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?



Solution 1:[1]

Try accessing the button text using setValue, like this:

Swift 4

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

it works for me :)

Solution 2:[2]

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

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

Solution 3:[3]

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 4:[4]

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>)
}

Solution 5:[5]

I solved it with finding UIButton pointer in UISearchBar object. I have objective-c code from my project, but you can see the logic.

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

    [searchBar setShowsCancelButton:YES animated:YES];

    for (UIView *ob in ((UIView*)searchBar.subviews[0]).subviews) {
        if ([ob isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton*)ob;
            [btn setTitle:@"?ptal" forState:UIControlStateNormal];
        }
    }
    return YES;
}

Solution 6:[6]

First of all you need to show custom search bar button in navigation controller like

in ViewDidLoad()

self.searchDisplayController?.displaysSearchBarInNavigationBar = true

and in Delegate Method

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

    var barButton = UIBarButtonItem(title: "Button Title", style: UIBarButtonItemStyle.Done, target: self, action: "here")
    self.searchDisplayController?.navigationItem.rightBarButtonItem = barButton
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Arun Kumar
Solution 3
Solution 4 Stanislav M.
Solution 5
Solution 6 Community