IOS Change the name of the cancel button in UISearchBar

I want to change the name of the cancel button in iOS. I used this before:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{ self.searchDisplayController.searchBar.showsCancelButton = YES; UIButton *cancelButton = nil; for (UIView *subView in self.searchDisplayController.searchBar.subviews) { if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) { cancelButton = (UIButton*)subView; } } [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal]; } 

But it does not seem to work in iOS7. Any suggestions?

+8
ios uisearchbar
source share
13 answers

just execute the code for this: -

 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { /* when user start editing in serchbar this method will display cancel button and disable the autocorrection functionality */ srcbar.showsCancelButton = YES; for (UIView *subView in searchBar.subviews) { if ([subView isKindOfClass:[UIButton class]]) { UIButton *cancelButton = (UIButton*)subView; [cancelButton setTitle:@"hi" forState:UIControlStateNormal]; } } srcbar.autocorrectionType = UITextAutocorrectionTypeNo; } 

Do not test in iOS7, but it works great in iOS6, hope this works for you.

EXIT: -

enter image description here

+9
source share

You need to search for the button recursively. This should be a safe way:

 - (void)viewDidLoad { [super viewDidLoad]; [self convertButtonTitle:@"Cancel" toTitle:@"Annuller" inView:self.searchBar]; } - (void)convertButtonTitle:(NSString *)from toTitle:(NSString *)to inView:(UIView *)view { if ([view isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton *)view; if ([[button titleForState:UIControlStateNormal] isEqualToString:from]) { [button setTitle:to forState:UIControlStateNormal]; } } for (UIView *subview in view.subviews) { [self convertButtonTitle:from toTitle:to inView:subview]; } } 

I tested it only on iOS 7, but it works and should do it for iOS 6 too.

+12
source share

here is my solution for ios6 and ios7

 #define IS_IOS7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { self.searchDisplayController.searchBar.showsCancelButton = YES; UISearchBar *searchBar = self.searchDisplayController.searchBar; UIView *viewTop = IS_IOS7 ? searchBar.subviews[0] : searchBar; NSString *classString = IS_IOS7 ? @"UINavigationButton" : @"UIButton"; for (UIView *subView in viewTop.subviews) { if ([subView isKindOfClass:NSClassFromString(classString)]) { UIButton *cancelButton = (UIButton*)subView; [cancelButton setTitle:@"your title" forState:UIControlStateNormal]; } } } 
+10
source share

to make it work on iOS7, you need to search in a sub-item of the search bar:

 //iOS 7 hack searchBar.showsCancelButton = YES; UIView* view=searchBar.subviews[0]; for (UIView *subView in view.subviews) { if ([subView isKindOfClass:[UIButton class]]) { UIButton *cancelButton = (UIButton*)subView; [cancelButton setTitle:@"Anuluj" forState:UIControlStateNormal]; } } 
+8
source share

Put this line in appDelegate

[[Appearance of UIButtonWhenContainedIn: [UISearchBar class], nil] setTitle: @ "Your name" forState: UIControlStateNormal];

+3
source share

For Swift 3.0

This works fine.

 func setSearchButtonText(text:String,searchBar:UISearchBar) { for subview in searchBar.subviews { for innerSubViews in subview.subviews { if let cancelButton = innerSubViews as? UIButton { cancelButton.setTitleColor(UIColor.white, for: .normal) cancelButton.setTitle(text, for: .normal) } } } } 

And call the method

 setSearchButtonText(text: "Done", searchBar: yourSearchBar) 

Here is the conclusion

enter image description here

+3
source share

@Nitin Gohel How so?

 - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{ self.searchDisplayController.searchBar.showsCancelButton = YES; UIButton *cancelButton = nil; for (UIView *subView in self.searchDisplayController.searchBar.subviews) { if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) { cancelButton = (UIButton*)subView; [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal]; } } } 
0
source share

It seems that "self.searchDisplayController.searchBar.subviews" just returns a "searchBar". I tried a roar and it works!

 for (UIView *searBarView in [self.searchDisplayController.searchBar subviews]) { for (UIView *subView in [searBarView subviews]) { if ([subView isKindOfClass:[UIButton class]]) { UIButton *cancleButton = (UIButton *)subView; [cancleButton setTitle:@"hi" forState:UIControlStateNormal]; } } } 
0
source share

Before starting a search in UISearchBarDelagate or UISearchDisplayControllerDelegate, do:

 [self.searchDisplayController.searchBar.subviews enumerateObjectsUsingBlock:^(UIButton *subview, NSUInteger idx, BOOL *stop) { if ([subview isKindOfClass:[UIButton class]]) { [subview setTitle:@"OK" forState:UIControlStateNormal]; *stop = YES; } }]; 
0
source share

If you just want to change the Cancel text to the same in your locale, the correct way, I think, is to use localization. Here's how:

  • open your project in Finder, find the en.lproj directory
  • rename it to your locale or duplicate if you need to support multiple languages
  • remove any links to files that may be contained in Xcode (they should look red).
  • drag the same files from the Finder from the new directory into Xcode
  • (not sure if this is necessary) edit your Info.plist and add the following lines:

    • Localization native development region : your region (ex: en )
    • Localizations : e.g. English

Note: it does not work in Simulator, test it on the device!

(Source: http://www.ibabbleon.com/iphone_app_localization.html )

0
source share

For iOS7, this works.

 -(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{ self.searchDisplayController.searchBar.showsCancelButton = YES; UIButton *cancelButton; UIView *topView = self.searchDisplayController.searchBar.subviews[0]; for (UIView *subView in topView.subviews) { if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) { cancelButton = (UIButton*)subView; } } if (cancelButton) { //Set the new title of the cancel button [cancelButton setTitle:@"YourTitle" forState:UIControlStateNormal]; } } 
0
source share

Refer to @Salih's solution above, I used this code and it works great! Just call the setupAppearance method in AppDelegate.m when the application starts.

 - (void)setupAppearance { id appearance = nil; if (IOS9_OR_LATER) { appearance = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]; } else { appearance = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]; } [appearance setTitle:@"CANCEL"]; } 
0
source share

Interacting with the code I used inside the protocol method of UIKitUISearchResultsUpdating updateSearchResults(for searchController: UISearchController) :

 if let cancelButton : UIButton = searchController.searchBar.value(forKey: "_cancelButton") as? UIButton { cancelButton.setTitle("Back", for: UIControlState.normal) } 

(iOS 10, Swift 3.0)

0
source share

All Articles