Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UISearchBar cancel button text in iOS 8

I'd like to change the text from "Cancel" to "Done" of the Cancel button inside the UISearchBar in iOS 8. I am using UISearchController. I've tried different approaches for iOS 6 and iOS 7 and they do not work. Has anybody done this?

like image 443
Wilmer Avatar asked Mar 11 '15 15:03

Wilmer


2 Answers

Objective-C:

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

Swift:

searchBar.setValue("customString", forKey:"_cancelButtonText") 
like image 171
Burhanuddin Sunelwala Avatar answered Sep 17 '22 16:09

Burhanuddin Sunelwala


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

like image 22
polo987 Avatar answered Sep 17 '22 16:09

polo987