self.navigationItem.backBarButtonItem 无法设置

self.navigationItem.backBarButtonItem cannot be set

我有一个名为 TVC 的 TableViewController 和其中的一个单元格。当我单击单元格时,TableViewController 会将新的 ViewController (VC) 推送到其导航控制器中。在这种情况下,我在 TVC 的 viewDidLoad 函数中设置了 [self.navigationItem.backBarButtonItem setTitle:@" "]。但是在新推送的 ViewController VC 中,后退按钮仍然有 "back" 文本。谁能解释一下?

顺便说一句,TableViewController TVC 也被另一个视图控制器 VC2 推送。我在 VC2 的 viewDidLoad 函数中设置了相同的函数 [self.navigationItem.backBarButtonItem setTitle:@" "],TVC 中显示的后退按钮文本被正确隐藏。

那是因为后退按钮采用了前一个视图控制器的标题。当 none 设置时,它默认返回。所以将 TableViewController 的名称设置为空字符串,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = ""
}

这是我的例子:

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[leftView setBackgroundColor:[UIColor clearColor]];
UIButton *leftViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftViewButton setFrame:CGRectMake(0, 0, 30, 30)];
[leftView addSubview:leftViewButton];

[leftViewButton setImage:[UIImage imageNamed:@"NavigationBarButtonBackDefault"] forState:UIControlStateNormal];
[leftViewButton setImage:[UIImage imageNamed:@"NavigationBarButtonBackPressed"] forState:UIControlStateHighlighted];

UIBarButtonItem *barButtonItemLeft = [[UIBarButtonItem alloc] initWithCustomView:leftView];
self.navigationItem.leftBarButtonItem = barButtonItemLeft;

来自文档:

If the title of your back button is too long to fit in the available space on the navigation bar, the navigation bar may substitute the string “Back” in place of the button’s original title. The navigation bar does this only if the back button is provided by the previous view controller. If the new top-level view controller has a custom left bar button item—an object in the leftBarButtonItems or leftBarButtonItem property of its navigation item—the navigation bar does not change the button title.

Bimala 和 Ted 指出了解决该问题的两种方法。前者是设置一个自定义的BackButtonItem,后者是简单地使用默认的BackButton。但是将屏幕的标题设置为“”可能并不总是可以接受的。所以你可以选择在 ViewWillDisappear 方法中做同样的事情。

OBJ C:

-(void) viewWillDisappear:(BOOL)animated{
    self.navigationItem.title = @"";
    [super viewWillDisappear:animated];
}

SWIFT:

override func viewWillDisappear(animated: Bool) {
    self.title = ""
    super.viewWillDisappear(true)
}

您也可以在 ViewWillappear 中重新设置标题

OBJ C:

-(void)viewWillAppear:(BOOL)animated  {  
    [super viewWillAppear:animated];
    self.navigationItem.title = @"your title"; 
}

SWIFT:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)
    self.title = "your title"
}

如果您只想隐藏后退按钮,请尝试:

self.navigationItem.hidesBackButton = YES;

self.navigationController.navigationBar.topItem.hidesBackButton = YES;

如果您想设置自定义后退按钮,请使用:

UIBarButtonItem *barButtonItem = [ [UIBarButtonItem alloc] initWithTitle: @"Custom"
                                                                   style: UIBarButtonItemStylePlain
                                                                  target: nil action: nil];
self.navigationController.navigationBar.topItem.backBarButtonItem = barButtonItem;