Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController change 'Cancel' button title

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

enter image description here

like image 891
Almudhafar Avatar asked Feb 21 '15 06:02

Almudhafar


3 Answers

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

[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];
like image 151
Burhanuddin Sunelwala Avatar answered Nov 12 '22 10:11

Burhanuddin Sunelwala


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!

like image 11
NBoymanns Avatar answered Nov 12 '22 10:11

NBoymanns


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;
        }
    }
}
like image 8
user3575114 Avatar answered Nov 12 '22 11:11

user3575114