当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。

我想把按钮上显示的文本更改为其他内容。

我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。

self.navigationItem.leftBarButtonItem.title = @"Log Out";

我该怎么办?

谢谢。


当前回答

以下是答案:

在viewDidAppear:animated中(不是在viewDidLoad中)执行以下操作

- (void)viewDidAppear:(BOOL)animated
{
     [self.navigationController.navigationBar.backItem setTitle:@"anything"];

     // then call the super
     [super viewDidAppear:animated];
}

如果你想保持后退按钮的形状。

其他回答

问题:导航栏中的“返回”文本不能被替换。

原因:在推送视图后,导航栏中设置了“Back”标签,因为父视图控制器中的.title属性被设置为nil(或未初始化)。

一个解决方案:如果你设置self.title="Whatever…",你会看到在按下新的视图控制器后,"Back"将出现"Whatever…"。

我有一个父视图控制器,标题很长。这导致后退按钮的文本溢出到子视图控制器的标题中。

在尝试了一堆不同的解决方案后,这就是我最终所做的(扩展@john.k。doe方法):

使用Xcode 7.2, Swift 2

在故事板中,添加一个导航项到父视图控制器场景(不是子VC)

在新导航项的属性检查器上,在后退按钮字段中键入空格字符。稍后再详细介绍。

在父视图控制器中,添加以下代码:

代码片段:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    switch segue.destinationViewController {
    case is ChildViewController:
        navigationItem.backBarButtonItem?.title = ""
    default:
        navigationItem.backBarButtonItem?.title = "Full Parent Title"
    }
}

解释:

后退按钮属于父视图控制器。Navigation Item为您提供了返回按钮的句柄,因此您可以在代码或故事板中设置标题。

注意:

如果你保留导航项返回按钮文本为默认空字符串,返回按钮标题将变成“返回”。

其他方法也管用,为什么要用这个呢?:

虽然在子视图控制器上重写back按钮标题是可能的,但在它已经在屏幕上短暂闪烁之前获得它的句柄是一个挑战。

有些方法构造一个新的后退按钮并覆盖现有的后退按钮。我确信它可以工作,并且在某些用例中可能是必要的。但我更喜欢在可能的情况下利用现有的api。

在某些情况下,改变父视图控制器的标题是最快的解决方案。但是,这会改变父标题,因此您必须管理状态。标签栏控制器也会变得混乱,因为标题更改会对标签栏项目标题产生副作用。

以下是答案:

在viewDidAppear:animated中(不是在viewDidLoad中)执行以下操作

- (void)viewDidAppear:(BOOL)animated
{
     [self.navigationController.navigationBar.backItem setTitle:@"anything"];

     // then call the super
     [super viewDidAppear:animated];
}

如果你想保持后退按钮的形状。

Most of solutions kills the original style of BackButton (The left arrowed bar button) while adding a usual button with desired title. So to keep the original style there are 2 ways: 1st: To use undocumented button style (110 or something like that) which I prefer not to do. But if you want you could find how to do it here, on stackoverflow. 2nd: To use I the Trenskow's idea. I liked it and I use it a bit changed. Instead of overriding - (NSString*)title I've decided to keep the original title in the following way (which allows me to use nib's titles as well as given title at push state btw).

- (void)viewDidLoad {
    [super viewDidLoad];
    static NSString * backButtonTitle=@"Back"; //or whatever u want

    if (![self.title isEqualToString:backButtonTitle]){

        UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
        customTitleView.text = self.title; // original title
        customTitleView.font = [UIFont boldSystemFontOfSize:20];
        customTitleView.backgroundColor = [UIColor clearColor];
        customTitleView.textColor = [UIColor whiteColor];
        customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
        customTitleView.shadowOffset = CGSizeMake(0, -1);

        [customTitleView sizeToFit];

        self.navigationItem.titleView = [customTitleView autorelease];
        self.title = backButtonTitle; 
    }
}

这个解决方案效果很好,看起来很自然。此外,如果在viewDidLoad方法中使用它,它可以防止执行超过1次。 我也试过杰塞德克的解决方案,但看起来很糟糕。它导致可见的用户标题栏的变化,从原来的BackButton的期望和回来。

这应该放在调用名为NewTitle的ViewController的方法中。 就在push或popViewController语句之前。

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];