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


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


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.