Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


Same in Xcode 6 Beta 7.

e.g. in the screenshot below, i want the Back button for the view controller(s) that i push to have "Back" as the title of the Back button.

in Xcode 4.5 using storyboard, by far the easiest solution i've found when the value of the Back button doesn't have to change dynamically is to use the "Back Button" field associated with the Navigation Item of the View Controller to which you want the "Back" button to say something else.

of course, this won't work if you need the back button to say something slightly different each time there are all of the other solutions here for that.

Note
Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


self.title = @"Really Long Title";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Short" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

"When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. [...] If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead."

Giving a custom title to the button.

Good answer! But what's the purpose of this behavior?

Here is the documentation for backBarButtonItem:

When any other view controller B is on top of the navigation stack, and A is right below it, B's back button will have the title "Short".

the tricky part is you need to set the back button in the PARENT view controller and not the child view. Wasted an hour just figured this out.

Note
Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


// Rename back button
    let backButton = UIBarButtonItem(
        title: "Back",
        style: UIBarButtonItemStyle.Plain, // Note: .Bordered is deprecated
        target: nil,
        action: nil
    )
    self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton
Note
Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


@implementation CustomViewController

- (NSString*)title {
    return @"Back Button Title";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
    customTitleView.text = @"Navigation Bar Title";
    customTitleView.font = [UIFont boldSystemFontOfSize:20];
    customTitleView.backgroundColor = [UIColor clearColor];
    customTitleView.textColor = [UIColor whiteColor];
    customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    customTitleView.shadowOffset = CGSizeMake(0, -1);

    [customTitleView sizeToFit];

    self.navigationItem.titleView = [customTitleView autorelease];
}

@end

EDIT: This also maintains the back button native look (The left arrowed bar button item).

I've found, that the easiest way to change the name of the back button is to set the view controllers title to the title of the back button, and then replacing the titleView in the view controllers navigation item to a custom label with it's real name.

In the case of view controller A and B, A is responsible for telling how it's back button should look, while B is displayed.

This will make your title in UINavigationBar look as if it was native. Giving the view controller the ability to have seperated title and back button title.

Note
Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];
-(IBAction)OnClick_btnBack:(id)sender  {
      [self.navigationController popViewControllerAnimated:YES];
    //[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}

A leftBarButtonItem does not have the appearance of a backBarButtonItem, if that is important in the application design.

After you do that, you need to write to code for "btnBack" action in the same .m file like this;

Of the solutions posted (thank you very much) this one seems to work, albeit as Alex stated above, the left bar button doesn't look like a back bar item. How can I make it look like a back bar item???

Ok, here is the way. If you have a view controller "first" and you navigate another view controller "second" by pushing a button or etc. you need to do some work. First you need to create a BarButtonItem in "second" view controller's ViewDidLoad method like this;

This really worked for me ..Thanks a lot ..

Note
Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


#import "MyCustomNavController.h"


@implementation MyCustomNavController {

    NSString *_savedTitle;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated withBackBtnTitle:(NSString *)title {
    _savedTitle = self.topViewController.title;

    self.topViewController.title = title;
    [super pushViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {

    [self.viewControllers objectAtIndex:self.viewControllers.count - 2].title = _savedTitle;
    return [super popViewControllerAnimated:animated];
}

@end

Im new in iOS but I will provide my very simple answer of overriding the navigation controller class. I have simple override the push and pop methods and save the title of previous view controller. Sorry for pasting in js block. Was little confused how to past it in normal code block.

Note
Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }
+ (void)makeSubViewHaveBackButton:(UIViewController*) view_controller{
  static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }
}

In myself use, I make it a function like this, just have the feature with one line code in the viewDidLoad.

Stan's answer was the best one. But it also have a problem, when you use the controller with a Tab Bar and change the controller's title, you could change the Tab Bar's title too.So the best answer is change the view_controller.navigationItem.title only and use the view_controller.navigationItem.title in the function. Answer is here:(With ARC and add them into view's viewDidLoad)

Note
Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];

backButton.title = @"Go Back";

self.navigationItem.backBarButtonItem = backButton;
backButton.title = NSLocalizedString(@"Back Title", nil);

Remember to put the code inside the parent view controller (e.g. the view that has your table view or UITableViewController), not the child or detail view (e.g. UIViewController).

This works for me as a "simplified" version of previous posted answers.

You can easily localize the back button string like this:

Note
Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


Simpliest solution. Works on both iOS 6 and 7. Put in the base UIViewController and done. Much better than the IB solution or setting in the pushing vc.

Tested and verified on iOS 7 in Xcode 5.0.2. This WILL change the previous screen's title to Back when you go back to it though.

it changes title of previous view controller, that's the problem in this solution

Note
Rectangle 27 0

iphone How do I change the title of the "back" button on a Navigation Bar?


UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];

Jordan says he "[added] a little more clarity on where to put the code." Here is even more clarity: put it in the first (aka parent) controller, immediately before the call to push the second (aka child) controller.

This should be placed in the method that calls the ViewController titled "NewTitle". Right before the push or popViewController statement.

To change the back button for my third level view controller (second push) I had to use the following line instead of the above answer: [[self.parentViewController navigationItem] setBackBarButtonItem: newBackButton]; Am I doing something wrong?

When in the child view controller, calling this from the child view controller will not work. You have to call this from the parent view controller, while within the child view controller.

You don't have to do this right before push or pop. You can do it in viewDidLoad or loadView or wherever else you set the title and other properties for this view. Victor Bogdan's answer below is clearer, IMHO.

you can create a custom BaseViewController and put it in to -viewDidLoad:

Note