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

207,349

Solution 1

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

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

Solution 2

In ChildVC this worked for me...

self.navigationController.navigationBar.topItem.title = @"Back";

Works in Swift too!

self.navigationController!.navigationBar.topItem!.title = "Back"

Solution 3

Here is the documentation for backBarButtonItem:

"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."

View Controller A (the "parent" view controller):

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

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".

Solution 4

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.

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.

enter image description here

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.

Solution 5

I know, the question is very old, but I found a nice solution.

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Custom Title";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;

Works from childView! Tested with iOS 7.

Share:
207,349

Related videos on Youtube

Dale
Author by

Dale

Updated on November 08, 2021

Comments

  • Dale
    Dale over 2 years

    Currently the left bar button default value is the title of the view that loaded the current one, in other words the view to be shown when the button is pressed (back button).

    I want to change the text shown on the button to something else.

    I tried putting the following line of code in the view controller's viewDidLoad method but it doesn't seem to work.

    self.navigationItem.leftBarButtonItem.title = @"Log Out";
    

    What should I do?

    Thanks.

    • Lal Krishna
      Lal Krishna over 3 years
      Please check iOS 11+ solution - Doesn't require to create backbutton again.
  • Alex Reynolds
    Alex Reynolds almost 15 years
    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.
  • Alex Reynolds
    Alex Reynolds almost 15 years
    A leftBarButtonItem does not have the appearance of a backBarButtonItem, if that is important in the application design.
  • Dale
    Dale almost 15 years
    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???
  • Jordan
    Jordan almost 15 years
    Then make this the answer because the one the one you chose is technically incorrect.
  • Dimitris
    Dimitris over 14 years
    Or in one line: self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"") style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease]; Thanks!
  • JonB
    JonB about 14 years
    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?
  • benvolioT
    benvolioT over 13 years
    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.
  • MiQUEL
    MiQUEL about 11 years
    this is the best solution if you don't care about localization.
  • ArtFeel
    ArtFeel almost 11 years
    you can create a custom BaseViewController and put it in to -viewDidLoad:
  • Joe Strout
    Joe Strout over 10 years
    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.
  • Alex Zavatone
    Alex Zavatone over 10 years
    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.
  • Eric Chen
    Eric Chen over 10 years
    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.
  • Nikita
    Nikita over 10 years
    it changes title of previous view controller, that's the problem in this solution
  • Travis M.
    Travis M. about 10 years
    Not bad except that they can see it change titles. I tried in viewWillAppear and it doesn't work.
  • Warren Burton
    Warren Burton over 9 years
    swift 1.1 syntax is self.navigationController?.navigationBar.topItem?.backBarBut‌​tonItem
  • narco
    narco almost 9 years
    This only seems to work to one level. It is not working for a child of a child for me. iOS 8.
  • Dan Beaulieu
    Dan Beaulieu over 8 years
    @Nikita, You can set the tile back in viewWillDisappear
  • Septronic
    Septronic over 8 years
    I'm using this in iOS 8.4 and 9. I'm doing this just before I push the new viewcontroller (I'm doing it through segue), and it works beautifully.
  • Ian Pinto
    Ian Pinto over 8 years
    Above solution works, except do not need last line in ARC mode. 'release' is unavailable in automatic reference counting mode.
  • Kiryl Bielašeŭski
    Kiryl Bielašeŭski over 8 years
    Use UIBarButtonItemStylePlain when minimum deployment target is iOS7 or later. UIBarButtonItemStyleBordered is deprecated.
  • Merkurial
    Merkurial about 8 years
    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.
  • gravitron
    gravitron about 8 years
    this will actually change the title of the view controller previous in the stack to "TEXT", which is less than ideal.
  • Micro
    Micro about 8 years
    Style should be UIBarButtonItemStyle.Plain
  • Womble
    Womble over 7 years
    Thanks for this. What a truly pathetic API this is. And representative of UIKit in general. Between navigation bars and Status Bars in iOS, it's a wonder we still develop for this goddamned platform. Thank baby jesus for Stack Overflow and its contributors!
  • AnBisw
    AnBisw over 6 years
    Man, i gotta tell you, it's 2017 and it's still a pain in the a$$ to handle the back button. Your method worked for me and I ended up with the "<" sign although I wanted the word "Cancel", but I will settle for what I got. :)
  • Illya Krit
    Illya Krit over 6 years
    According to your blog post: override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() let customBackButton = UIBarButtonItem(title: "BackTitle", style: .bordered, target: nil, action: nil) self.navigationController?.navigationBar.backItem?.backBarBu‌​ttonItem = customBackButton }
  • villapossu
    villapossu about 6 years
    The original question didn't say anything about using storyboards, so it's definitely good this is not the accepted answer, however correct a solution this is for storyboard-based development.
  • heyfrank
    heyfrank almost 6 years
    @Wobmle: You should develop for Android for a few months ;)