UINavigationController如何设置标题

gri*_*lix 22 uinavigationcontroller ios

我有一个控制器/视图的通用项目列表,可以扩展显示自定义列表..列表和导航工作正常..但我无法更改UINavigationController的标题.在通用控制器中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview: navigationController.view];
}
- (void)setNavigationTitle: (NSString *)title
{
    NSLog(@"set title: %@", title); // this works
    self.navigationController.title = title; // Nothing works here
}
Run Code Online (Sandbox Code Playgroud)

然后,扩展类做..

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNavigationTitle: @"Custom list"];
}
Run Code Online (Sandbox Code Playgroud)

navigationBar仍然有"Item"作为标题:(

wil*_*ood 58

在你的UIViewController有一个title属性,它是由将显示的属性NavigationController.因此,当将新推UIViewController送到导航堆栈时,将其标题设置UIViewController为适当的标题.

在你的情况下,它看起来像是:

[self setTitle:@"WhateverTitle"];
Run Code Online (Sandbox Code Playgroud)


fie*_*edl 10

对于那些寻找Swift解决方案的人:

class CustomViewController: SuperViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "My Custom Title"
  }
}
Run Code Online (Sandbox Code Playgroud)

title需要被所设定的UIViewController,而不是在UINavigationController嵌入视图控制器.

文档:UIViewController类参考:title属性


Gya*_*ani 5

使用

self.title = @"yourTitle";
Run Code Online (Sandbox Code Playgroud)