Change UISearchBar cancel button text in iOS 8 Change UISearchBar cancel button text in iOS 8 ios ios

Change UISearchBar cancel button text in iOS 8


Objective-C:

[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];

Swift:

searchBar.setValue("customString", forKey:"_cancelButtonText")


This worked for me in ios8 through ios13, did not try in ios7, but should do the trick, beware of placing this line in the early times of the app cycle (eg : appDelegate)

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"Annuler"];

and as of ios9 you could also use

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"Annuler"];

Swift version:

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

Hope that helps ;)


Found it!. in iOS 8, the cancel button is under the key "cancelButton". I used UISearchBar delegate searchBarTextDidBeginEditing to make the change.

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {    let uiButton = searchBar.valueForKey("cancelButton") as UIButton    uiButton.setTitle("Done", forState: UIControlState.Normal)}