Show Menu

Looking for an iOS Developer?

Submit your 30 day Job Listing for FREE

Updated for iOS7, If you are writing a iOS application that contains of UITableView along with a UISearchBar, you might want to change the title of the cancel button, i.e. if you’re localizing your app.

But the cancel button isn’t that trivial to get a hold of. Here is some code that allows you to change the title:
Implement the

searchDisplayControllerWillBeginSearch

method and have it contain the following:

   
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    /* when user start editing in search bar 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;

}

The above code assumes that your class is a

UISearchDisplayDelegate

This is the Output:

This post was developed further with the help from Stackoverflow and the image also supplied by user: Nitin Gohel

If you’re using iOS7 the following code will do the trick:


-(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:@"Hi" forState:UIControlStateNormal];
    }
}

Check out the beginners guide to objective-c if you would like to strengthen your knowledge and start your journey in becoming an iOS Developer

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

IT developer situated in Silkeborg, Denmark. Working primarily with Java, and web technologies. Got a profound passion for mobile development, on the iOS platform.

Comments

There are currently: 5 Responses to “Quick iOS Tip: Change the title of cancel button in UISearchBar”. Leave your comment!

I updated the post with iOS7 compatible code


Thanks. But this doesn’t work in iOS 7 anymore. Maybe you have the solution?


    Try putting

    [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];

    inside the if statement. Like 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];
          }
       }
       
    }
    

Thanks. Just what I needed.



Leave a Reply

Would you like to sign up to the mailing list by our sister site: SwiftMonthly?