-->

How to change the UINavigationController back butt

2020-01-27 02:06发布

问题:

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.

回答1:

In viewWillAppear write this

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

And in ViewWilldisapper write this

self.navigationItem.title = @"Back";

It works without story boards.



回答2:

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.

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.



回答3:

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:

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



回答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";
});


回答5:

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:



回答6:

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



回答7:

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


回答8:

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!



回答9:

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];
}


回答10:

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.



回答11:

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")
}


回答12:

Try Below Code :

[self.navigationItem setValue:@[@"customTitle",@""] forKey:@"abbreviatedBackButtonTitles"];


回答13:

Actually, you can change title of back button:

self.navigationItem.backBarButtonItem.title = @"NewName";


回答14:

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.

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



回答15:

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.



回答16:

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.



回答17:

Swift 4

In your UIViewController, set this:

let backButton = UIBarButtonItem()
backButton.title = "[Your title here]"
self.navigationItem.backBarButtonItem = backButton