UISearchController change 'Cancel' button title
Asked Answered
L

11

7

How we can change the title of cancel button in search controller?

enter image description here

Loquat answered 21/2, 2015 at 6:16 Comment(1)
I have written an answer to this topic here: #19207257. Just use the SHSearchBar Cocoapod which is not such a pain in the ass like the UISearchBar.Savell
E
8

You can change the "Cancel" Button in search bar using this-

for (UIView *view in searchBar.subviews)
{
    for (id subview in view.subviews)
    {
        if ( [subview isKindOfClass:[UIButton class]] )
        {
            [subview setEnabled:YES];
            UIButton *cancelButton = (UIButton*)subview;
            [cancelButton setTitle:@"hi" forState:UIControlStateNormal];


            NSLog(@"enableCancelButton");
            return;
        }
    }
}
Erickson answered 21/2, 2015 at 7:49 Comment(0)
R
18

The solution provided above could change with new iOS releases. Here is a better approach:

[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];
Runnerup answered 25/3, 2015 at 10:53 Comment(5)
_cancelButtonText is private, it may also change with new iOS releases if Apple decides to rename it, and because it's private they can safely presume that nobody is using this name in their appsUnrelenting
I found this value can only be changed once as well. Any other solution where I can change the text (language) dynamically?Crelin
@JoshuaVidamo no its not only once, whenever you will call the above method, it will be changed. What exactly is your question?Runnerup
This throws an exception when built on the iOS 13 SDK and the app is run on iOS 13. Is there an updated solution?Simas
@Simas use the Appearance solution mentioned above.Intermediate
S
14

The Swift equivalent of Burhanuddin Sunelwala answer did the trick for me!

self.searchBar.setValue("custom string", forKey: "cancelButtonText")

Thanks to Burhanuddin Sunelwala for putting me in the right direction!

Smitherman answered 2/8, 2016 at 13:44 Comment(1)
Worked for me, iOS 15, XCode 15Midwife
E
8

You can change the "Cancel" Button in search bar using this-

for (UIView *view in searchBar.subviews)
{
    for (id subview in view.subviews)
    {
        if ( [subview isKindOfClass:[UIButton class]] )
        {
            [subview setEnabled:YES];
            UIButton *cancelButton = (UIButton*)subview;
            [cancelButton setTitle:@"hi" forState:UIControlStateNormal];


            NSLog(@"enableCancelButton");
            return;
        }
    }
}
Erickson answered 21/2, 2015 at 7:49 Comment(0)
S
4

Worth noting, that the preferred method for changing the Cancel button title is now via appearances (got the idea from an another question: https://mcmap.net/q/365955/-change-uisearchbar-cancel-button-text-in-ios-8):

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"Annuler"];
Siamese answered 28/10, 2016 at 12:42 Comment(0)
M
3

You should use the appearance proxy of the UISearchBar. You can find an example here - How to change the default text of Cancel Button which appears in the UISearchBar +iPhone

Motionless answered 24/2, 2015 at 22:48 Comment(0)
B
2

swift version:

for view:UIView in (searchView?.subviews)!
{
        for subView:UIView in (view.subviews)
        {
            if ( subView is UIButton )
            {
                let cancelBut = subView as! UIButton

                //do stuff with cancelButton here
            }
        }
    }
Bavardage answered 22/2, 2016 at 11:15 Comment(0)
L
1
for (UIView *subView in SearchBar.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        UIButton *cancelButton = (UIButton*)subView;

        [cancelButton setTitle:@"TitleString" forState:UIControlStateNormal];
}
Lyonnesse answered 12/2, 2016 at 7:48 Comment(0)
S
1
  class ViewController: UIViewController,UISearchResultsUpdating {
        func updateSearchResults(for searchController: UISearchController) {
            //write your code here
        }
        @IBOutlet weak var navItem: UINavigationItem!
        let searchController = UISearchController(searchResultsController: nil)
        override func viewDidLoad() {
            super.viewDidLoad()
            searchController.obscuresBackgroundDuringPresentation = false
            searchController.definesPresentationContext = true
            searchController.searchResultsUpdater = self
            if #available(iOS 11.0, *){
                navigationItem.searchController = searchController
                UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "new title"
            }
            // Do any additional setup after loading the view.
        }
    }
Spheno answered 30/7, 2019 at 13:15 Comment(0)
L
1

swift 4 & 5

The best and simple way that worked for me is this snippet of code:

if let cancelButton = searchBar.value(forKey: "cancelButton") as? UIButton {
    cancelButton.setTitle("Annuler", for: .normal)
}

You can modify more properties like this:

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

I hope this helps :)

Lockwood answered 22/5, 2020 at 2:33 Comment(0)
P
0

You also need to have the searchBar setShowsCancelButton before the procedure.

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    [theSearchBar setShowsCancelButton:YES animated:NO];
    for (UIView *subView in theSearchBar.subviews){
        if([subView isKindOfClass:[UIButton class]]){
            [(UIButton*)subView setTitle:@"Done" forState:UIControlStateNormal];
        }
    }
}

Note also use UIButton to avoid problems with Apple!

Proteus answered 21/2, 2015 at 7:13 Comment(0)
S
0

Swift 4.2, 4.0+

A custom class that supports many customizations with the below results is added in the answer here.

enter image description here

Siesta answered 16/7, 2018 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.