ios 如何更改 UINavigationController 后退按钮名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9871578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:32:24  来源:igfitidea点击:

How to change the UINavigationController back button name?

iosuiviewcontrolleruinavigationcontrolleruinavigationitem

提问by Bulla

I have a UIViewControllerand 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 navigationcontrollerfor going back ....

我有一个UIViewController,我正在从我的第一个视图控制器导航到第二个视图控制器,我想更改显示在navigationcontroller返回按钮的名称......

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

now in the second viewcontrollerI want change the name of the button in the navigationController.

现在在第二个viewcontroller我想更改navigationController.

采纳答案by Tendulkar

In viewWillAppear write this

在 viewWillAppear 写这个

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

And in ViewWilldisapper write this

在 ViewWilldisapper 中写下这个

self.navigationItem.title = @"Back";

It works without story boards.

它在没有故事板的情况下工作。

回答by Mick MacCallum

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

如果您希望以编程方式执行此操作,可以这样做:

Objective-C

目标-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.

但是,如果您更喜欢使用 Interface Builder,只需选择您希望为其设置后退按钮的 UINavigationItem,然后导航到属性检查器以更改后退按钮的标题。

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.

注意:重要的是要记住,您必须在点击后退按钮时返回的视图控制器上配置它,而不是当前可见的视图控制器。

回答by Roger

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:

在 Interface Builder 或 Storyboarding 中有一种简单的方法可以做到这一点。在父视图控制器中,只需将导航项的属性“后退按钮”设置为您想要的任何标题。下面是一个例子:

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

Villian 的 Tavern View 将“Back”作为后退按钮标题,就像我们刚刚在父导航控制器(Best Bars)的属性检查器中设置的一样。

回答by neowinston

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

回答by Anil

Swift 3:

斯威夫特 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:

注意:后退按钮属于前一个视图控制器,而不是当前显示在屏幕上的那个。要修改后退按钮,您应该在推送之前在启动 segue 的视图控制器上更新它:

回答by Zaid Pathan

In SwiftI found solution very simple,

Swift 中,我发现解决方案非常简单,

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

假设我在ViewControllerA 上并转到ViewControllerB,如果我想更改ViewControllerB 上显示的后退按钮的名称,我将在ViewControllerA 中执行此操作,

self.title = "BackButtonTitle"

That's it.

就是这样。

Note:-This will change title of ViewControllerA

注意:-这将更改ViewControllerA 的标题

回答by said altintop

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.

我在 ViewController1 上,然后转到 ViewController2,如果我想更改 ViewController2 上显示的后退按钮的名称,我将在 ViewController2 中执行此操作。

Swift 5

斯威夫特 5

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

Objective-C

目标-C

self.navigationController.navigationBar.topItem.title = @"BackButtonTitle";

回答by Leojin

You can achieve this by using titleViewof navigationItem

您可以使用此实现titleViewnavigationItem

Swift 3 Answer

斯威夫特 3 个回答

? Create one extension for UINavigationItemas Below

? 创建一个扩展UINavigationItem如下

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 viewDidLoaddo the following

? 在您的控制器中viewDidLoad执行以下操作

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

回答by Hussain Shabbir

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

回答by CharmingQin

Try Below Code :

尝试以下代码:

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