How to change the UINavigationController back button name?
Asked Answered
S

18

60

I have a UIViewController and I'm navigating from my first view controller to second view controller and I want to change the name of the button shown on the navigationcontroller for going back ....

SecondViewController *secondController = [[SecondViewController alloc]
                                              initWithNibName:nil
                                              bundle:NULL]; 
[self.navigationController pushViewController:secondController  animated:YES];

now in the second viewcontroller I want change the name of the button in the navigationController.

Sherburn answered 26/3, 2012 at 11:51 Comment(0)
I
35

In viewWillAppear write this

self.navigationItem.title = @"List View";

And in ViewWilldisapper write this

self.navigationItem.title = @"Back";

It works without story boards.

Interdisciplinary answered 26/3, 2012 at 12:6 Comment(6)
Done this way you will see the title actually change before the new view (especially if you have animation). If you do it with viewDidDisappear, then you will see the button actually change. NSPostWhenIdle's solution is better. Originally sourced from: blog.evandavey.com/2008/12/…Drastic
This should not be the accepted answer. 0x7fffffff's answer is the supported method for doing this.Chumley
This does not work anymore. It's also very hacky. The answer of user716216 [https://mcmap.net/q/119357/-how-to-change-the-uinavigationcontroller-back-button-name] should be the right one.Shipworm
I have tried all the solutions but the only one works flawlessly everywhere is adding a bar button to navigation bar in designer or through code and set the custom selector manually believe me this is just three four lines of task but saves a lot of frustrationAlegre
This is a not a great answer because it is effectively working around – not with – the provided UIKit facilities for view controller bar buttons. see answer from @0x7ffffffff belowTruly
i dont know why everyone is hating against this answer... it may not be best practise but it's working just fine...Karlow
B
185

If you wish to do this programmatically, it can be done like this:

Objective-C

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"Custom"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:nil
                                                            action:nil];

[self.navigationItem setBackBarButtonItem:backItem];

Swift

let backItem = UIBarButtonItem(title: "Custom", style: .Bordered, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem

However, if you prefer using Interface Builder, just select the UINavigationItem that you wish to set the back button for, and navigate to the attributes inspector to change the back button's title.

enter image description here

NOTE: It is important to remember that you must configure this on the view controller that you would be returning to upon tapping the back button, not the currently visible view controller.

Belamy answered 26/3, 2012 at 12:4 Comment(8)
@Shady It's still there. Just select your UINavigationItem and you will see it in the "Attributes Inspector" tab in Interface Builder.Belamy
Thanks for this. Especially for the note in the end.Shipworm
You can combine the two approaches, programmatic and with storyboards. Add a dummy backButton title (as in the image shown) to the previous viewController in the hierarchy (so a backButton exists) and write self.navigationItem.backBarButtonItem.title = @"..."; to set the back title for the next viewControllerNgo
Also, in addition to the last note of putting it on the view controller that you are returning to, you need to also not have set the "Title" attribute, otherwise it will override the "Back Button" attribute.Yolandoyolane
The note is very important, should be with a higher font size and at the beginning of the answerPaillasse
since iOS8 UIBarButtonItemStyleBordered was deprecated and now UIBarButtonItemStylePlain should be used insteadFin
The note is very important!Bodine
thanks for this, accepted answer seems bit hacky, this actually should be the accepted answerMariannamarianne
G
42

There is an easy way of doing that in Interface Builder or Storyboarding. In the parent View Controller, just set the attribute "Back Button" of the navigation item to whatever title you want. Here is an example:

Set Back Button Title of child view controller form parent view controller

Villian's Tavern View will have "Back" as Back Button Title, like we just set in the attributes inspector of the parent's navigation controller (Best Bars).

Glochidiate answered 15/9, 2012 at 7:16 Comment(1)
You may have to add a navigation item to your view controller on the storyboard if it's not already there. To do that, select UINavigationItem from the object library and drag it onto your view controller on the story board.Micropaleontology
I
35

In viewWillAppear write this

self.navigationItem.title = @"List View";

And in ViewWilldisapper write this

self.navigationItem.title = @"Back";

It works without story boards.

Interdisciplinary answered 26/3, 2012 at 12:6 Comment(6)
Done this way you will see the title actually change before the new view (especially if you have animation). If you do it with viewDidDisappear, then you will see the button actually change. NSPostWhenIdle's solution is better. Originally sourced from: blog.evandavey.com/2008/12/…Drastic
This should not be the accepted answer. 0x7fffffff's answer is the supported method for doing this.Chumley
This does not work anymore. It's also very hacky. The answer of user716216 [https://mcmap.net/q/119357/-how-to-change-the-uinavigationcontroller-back-button-name] should be the right one.Shipworm
I have tried all the solutions but the only one works flawlessly everywhere is adding a bar button to navigation bar in designer or through code and set the custom selector manually believe me this is just three four lines of task but saves a lot of frustrationAlegre
This is a not a great answer because it is effectively working around – not with – the provided UIKit facilities for view controller bar buttons. see answer from @0x7ffffffff belowTruly
i dont know why everyone is hating against this answer... it may not be best practise but it's working just fine...Karlow
B
4

In my case it only worked running the code on the Main Thread, like so:

dispatch_async(dispatch_get_main_queue(), ^(void) {

    self.navigationController.navigationBar.backItem.title = @"Custom Title";
});
Berner answered 8/12, 2015 at 20:3 Comment(2)
This will change to all view controllers in hierarchy, so if you press back button, then, previous view controller title will also get affected. So, that case needs to be handled.Mg
Thanks for the comment.Berner
W
4

Swift 3:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Back"
    navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}

Note:The back button belongs to the previous view controller, not the one currently presented on screen. To modify the back button you should update it before pushing, on the view controller that initiated the segue:

Waistcloth answered 11/9, 2017 at 5:59 Comment(0)
D
3

In Swift I found solution very simple,

Suppose I'm on ViewControllerA and going to ViewControllerB, If I wants to change name of back button showing on ViewControllerB, I will do this in ViewControllerA,

self.title = "BackButtonTitle"

That's it.

Note:- This will change title of ViewControllerA

Dur answered 20/5, 2015 at 11:12 Comment(1)
This changes the title of ViewControllerA instead of chaning the back button on VCB.Bankroll
P
3

I'm on ViewController1 and going to ViewController2, If I wants to change name of back button showing on ViewController2, I will do this in ViewController2.

Swift 5

self.navigationController?.navigationBar.topItem?.title = "BackButtonTitle"

Objective-C

self.navigationController.navigationBar.topItem.title = @"BackButtonTitle";
Protectionism answered 26/6, 2019 at 10:17 Comment(0)
C
1

It is very important that in view controller "Simulated Metrics" option is not selected as "Inferred".

I tried with "None" and all it's right!

Cowley answered 18/4, 2013 at 12:4 Comment(0)
A
1

Try like this:-

NSArray *viewControllerArr = [self.navigationController viewControllers];
// get index of the previous ViewContoller
long previousViewControllerIndex = [viewControllerArr indexOfObject:self] - 1;
UIViewController *previous;
if (previousViewControllerIndex >= 0) {
    previous = [viewControllerArr objectAtIndex:previousViewControllerIndex];
    previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                 initWithTitle:@"Back"
                                                 style:UIBarButtonItemStylePlain
                                                 target:self
                                                 action:nil];
}
Arthralgia answered 3/5, 2015 at 11:27 Comment(0)
I
1

Actually, you can change title of back button:

self.navigationItem.backBarButtonItem.title = @"NewName";
Immanent answered 28/7, 2016 at 8:47 Comment(0)
B
1

If you use Storyboard references, the View Controller might not display the Navigation Item on Interface Builder.

Manually dragging a Navigation Bar will not work, you need to drag a Navigation Item

You'll then be able to set its back button's title. This is also where you'll set the view's title.

Quoting Mick MacCallum:

NOTE: It is important to remember that you must configure this on the view controller that you would be returning to upon tapping the back button, not the currently visible view controller.

Bettis answered 25/1, 2017 at 23:50 Comment(0)
A
1

You can achieve this by using titleView of navigationItem

Swift 3 Answer

• Create one extension for UINavigationItem as Below

extension UINavigationItem {
    func customTitle(title: String) {
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.textColor = UIColor.white
        titleLabel.textAlignment = .center
        titleLabel.sizeToFit()
        titleView = titleLabel
    }
}

• In your controllers viewDidLoad do the following

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "Text For Back Button"
        navigationItem.customTitle(title: "Title for your view controller")
}
Anibalanica answered 29/6, 2017 at 5:5 Comment(0)
H
1

Try Below Code :

[self.navigationItem setValue:@[@"customTitle",@""] forKey:@"abbreviatedBackButtonTitles"];
Haematosis answered 3/1, 2018 at 3:34 Comment(2)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewSainthood
Thanks for your advice, this is my second time to answer one question, I will learn more about the rules, and I will correct it next time.Haematosis
I
0

If you use Storyboard, there is a easy way to do this (it worked fine for me).

  • Go to your storyboard
  • Select which UIViewController you want to change the back button text.
  • Click on the UINavigationItem on the top of your UIViewController. (the one with the title)
  • On the Right panel, select the attribute inspector (the 4th icon).
  • Now you can set the Title, Prompt and Back Button texts.

enter image description here

Source: http://pinkstone.co.uk/how-to-change-the-back-button-text-on-a-uinavigationcontroller-in-your-storyboard/

Ilk answered 24/6, 2014 at 13:50 Comment(0)
D
0

There are two ways to do this.

First way is by program code:

Yakimenko Aleksey's answer is the simplest way, This really works ( tested on Xcode 8 + iOS 10)

Swift3:

self.navigationItem.backBarButtonItem?.title = "Your customized back title"

Note: you should call above code in source view controller, not the destination view controller.

Second way is use storyboard localization via Main.strings file,

"nnnnnnn.title" = "localized string";

The nnnnnnn means the object id of the BackBarButtonItem in the source view controller(not the destination view controller!)

You need set the "back" property of the navigation bar item, it will auto create a BarButtonItem, you find its object id.

Sorry, i failed to upload screenshots.

Denisdenise answered 12/4, 2017 at 14:34 Comment(0)
S
0

I had a rather complex storyboard with many navigation controllers, and my customer wanted all of the back buttons to say "Back"

I went through in Interface Builder and put "Back" into the Back button field of every UINavigationItem.

I was not working for every scene change!

The problem was that not every view controller in the storyboard had a UINavigationItem.

I fixed this by dragging out a new UINavigationItem onto every UIViewController that did not have one, and setting all of the Back button fields to "Back"

Watch out for the controllers that are not given their own UINavigationItem automatically.

Swellhead answered 7/2, 2018 at 6:42 Comment(0)
L
0

Swift 4

In your UIViewController, set this:

let backButton = UIBarButtonItem()
backButton.title = "[Your title here]"
self.navigationItem.backBarButtonItem = backButton
Lightly answered 14/2, 2018 at 17:0 Comment(0)
L
0

In Xcode 12 UINavigationItem has the backButtonTitle property

var backButtonTitle: String? { get set }
Lamee answered 22/6, 2020 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.