I\'d like to change the text from "Cancel" to "Done" of the Cancel button inside the UISearchBar
in iOS 8. I am using UISearchControll
In Swift:
searchBar.setValue("customString", forKey: "_cancelButtonText")
Based on Burhanuddin Sunelwala answer.
I have added titleLabel.font to the mix...
This goes straight into my ViewDidLoad() {
let searchFont = UIFont(name: "BrandonGrotesque-Medium", size: 12)
let textFieldSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldSearchBar.font = searchFont
let cancelButton = searchBar.valueForKey("cancelButton") as! UIButton
cancelButton.setTitle("Done", forState: .Normal)
cancelButton.titleLabel!.font = searchFont
Objective-C:
[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];
Swift:
searchBar.setValue("customString", forKey:"_cancelButtonText")
Here is Swift solution:
for (view) in _searchController.searchBar.subviews {
for (subview) in view.subviews {
if (subview.isKindOfClass(UIButton.self)) {
let cancelButton = subview as! UIButton
cancelButton.setTitle("BlaBla", forState: .Normal)
break
}
}
}
Swift 3.0:
In your AppDelegate add this:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "my text"
I was having trouble getting this to work for a UISearchBar within a UISearchController. The text didn't change the first time the cancel button was shown but it did the second time.
That is until I saw @polo987's answer. Here's what I did that worked:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Done"