Reputation: 887
How we can change the title of cancel button in search controller?
Upvotes: 7
Views: 10515
Reputation: 1663
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 :)
Upvotes: 1
Reputation: 11
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.
}
}
Upvotes: 1
Reputation: 2574
Worth noting, that the preferred method for changing the Cancel button title is now via appearances (got the idea from an another question: https://stackoverflow.com/a/34522163/511878):
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"Annuler"];
Upvotes: 4
Reputation: 716
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!
Upvotes: 14
Reputation: 3222
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
}
}
}
Upvotes: 2
Reputation: 789
for (UIView *subView in SearchBar.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)subView;
[cancelButton setTitle:@"TitleString" forState:UIControlStateNormal];
}
Upvotes: 1
Reputation: 5343
The solution provided above could change with new iOS releases. Here is a better approach:
[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];
Upvotes: 17
Reputation: 13343
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
Upvotes: 3
Reputation: 993
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;
}
}
}
Upvotes: 8
Reputation: 3283
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!
Upvotes: 0