How do I change the title of the "back" button on a Navigation Bar
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.
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.
Related videos on Youtube
Dale
Updated on November 08, 2021Comments
-
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 over 3 yearsPlease check iOS 11+ solution - Doesn't require to create backbutton again.
-
-
Alex Reynolds almost 15 yearsWhen 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 almost 15 yearsA
leftBarButtonItem
does not have the appearance of abackBarButtonItem
, if that is important in the application design. -
Dale almost 15 yearsOf 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 almost 15 yearsThen make this the answer because the one the one you chose is technically incorrect.
-
Dimitris over 14 yearsOr in one line: self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"") style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease]; Thanks!
-
JonB about 14 yearsTo 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 over 13 yearsJordan 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 about 11 yearsthis is the best solution if you don't care about localization.
-
ArtFeel almost 11 yearsyou can create a custom BaseViewController and put it in to
-viewDidLoad:
-
Joe Strout over 10 yearsYou 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 over 10 yearsTested 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 over 10 yearsSimpliest 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 over 10 yearsit changes title of previous view controller, that's the problem in this solution
-
Travis M. about 10 yearsNot bad except that they can see it change titles. I tried in viewWillAppear and it doesn't work.
-
Warren Burton over 9 yearsswift 1.1 syntax is
self.navigationController?.navigationBar.topItem?.backBarButtonItem
-
narco almost 9 yearsThis only seems to work to one level. It is not working for a child of a child for me. iOS 8.
-
Dan Beaulieu over 8 years@Nikita, You can set the tile back in viewWillDisappear
-
Septronic over 8 yearsI'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 over 8 yearsAbove solution works, except do not need last line in ARC mode. 'release' is unavailable in automatic reference counting mode.
-
Kiryl Bielašeŭski over 8 yearsUse UIBarButtonItemStylePlain when minimum deployment target is iOS7 or later. UIBarButtonItemStyleBordered is deprecated.
-
Merkurial about 8 yearsthe 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 about 8 yearsthis will actually change the title of the view controller previous in the stack to "TEXT", which is less than ideal.
-
Micro about 8 yearsStyle should be UIBarButtonItemStyle.Plain
-
Womble over 7 yearsThanks 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 over 6 yearsMan, 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 over 6 yearsAccording to your blog post: override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() let customBackButton = UIBarButtonItem(title: "BackTitle", style: .bordered, target: nil, action: nil) self.navigationController?.navigationBar.backItem?.backBarButtonItem = customBackButton }
-
villapossu about 6 yearsThe 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 almost 6 years@Wobmle: You should develop for Android for a few months ;)