How do I change the title of the "back" button on a Navigation Bar
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.
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".