How to change the UINavigationController back butt

2020-01-27 02:22发布

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.

17条回答
ゆ 、 Hurt°
2楼-- · 2020-01-27 03:00

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")
}
查看更多
【Aperson】
3楼-- · 2020-01-27 03:00

Try Below Code :

[self.navigationItem setValue:@[@"customTitle",@""] forKey:@"abbreviatedBackButtonTitles"];
查看更多
smile是对你的礼貌
4楼-- · 2020-01-27 03:01

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

查看更多
冷血范
5楼-- · 2020-01-27 03:10

In viewWillAppear write this

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

And in ViewWilldisapper write this

self.navigationItem.title = @"Back";

It works without story boards.

查看更多
叛逆
6楼-- · 2020-01-27 03:10

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/

查看更多
登录 后发表回答