Swift: searchBar - how to change "Cancel" Button title
Asked Answered
A

9

10

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?

Abner answered 4/4, 2015 at 11:21 Comment(0)
E
3
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)
        }
    }
}
Ely answered 4/4, 2015 at 15:59 Comment(1)
I don't use UISearchDisplayController it's deprecated. I use UISearchController. So this code cannot help me.Abner
A
29

Try accessing the button text using setValue, like this:

Swift 4

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

it works for me :)

Afterimage answered 3/4, 2018 at 15:15 Comment(1)
This worked in Swift 4, thanks! And it's way easier than the accepted answerMollymollycoddle
I
9

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

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "your button title"
Inbound answered 25/11, 2020 at 6:48 Comment(0)
F
6

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)
}
Fatality answered 14/3, 2021 at 11:44 Comment(0)
E
3
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)
        }
    }
}
Ely answered 4/4, 2015 at 15:59 Comment(1)
I don't use UISearchDisplayController it's deprecated. I use UISearchController. So this code cannot help me.Abner
M
2

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>)
}
Marylouisemaryly answered 18/6, 2020 at 14:32 Comment(1)
Your answer helped me! But how can I address the same to bookmarkButton?Concatenation
A
0

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;
}
Adjure answered 8/7, 2015 at 16:48 Comment(0)
N
0

You have to search subviews to find the UINavigationButton.

func findButton(in view: UIView, replace title: String) {
    for subview in view.subviews {
        if subview.isKind(of: NSClassFromString("UINavigationButton")!) {
            let cancelButton = subview as! UIButton
            cancelButton.setTitle(title, for: .normal)
            view.setNeedsLayout()
            break
        } else {
            findButton(in: subview, replace: title)
        }
    }
}

Usage:

findButton(in: searchBar, replace: title)
Nyeman answered 20/6, 2022 at 23:16 Comment(0)
E
0

Swift 5.9

I did

init(){

UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).title = "YourTitle"
}

Worked fine, this is the result:

SearchBar Custom text

Esurient answered 5/10, 2023 at 9:41 Comment(0)
M
-1

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
}
Martinez answered 4/4, 2015 at 11:33 Comment(2)
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.Abner
Please, I've already broke my brain in this hours... I don't use UISearchDisplayController it's deprecated. I use UISearchController.Abner

© 2022 - 2025 — McMap. All rights reserved.