想要更改NavigationItem标题

问题描述:

我已将第一个视图嵌入到NavigationController中,然后将segue设置为push。 我想在每个视图中更改导航栏的标题以显示某些内容并通过我的代码进行更新。 我试着拨打导航控制器是这样的:想要更改NavigationItem标题

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

但这不会工作,我该怎么办呢?

导航控制器获取从视图 - 控制标题所以 这将做的工作

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    self.title = @"Some Title"; 
} 

这个工作对我来说:

self.navigationItem.title = @"YourTitle"; 

希望它能帮助。

可以更换过标题视图:

UIView* titleView = [[UIView alloc] init]; 
[self.navigationItem setTitleView: titleView]; 

如果u只改变导航栏标题那就试试这个 上initwithnibname方法

 [email protected]"your title"; 

,但你想给一个颜色或字体大小那么你想采取一个有限的接口

 UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 

     label.backgroundColor = [UIColor clearColor]; 
     label.font = [UIFont boldSystemFontOfSize:20.0]; 
     label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
     label.textAlignment = UITextAlignmentCenter; 

     label.textColor = [UIColor blackColor]; 
     self.navigationItem.titleView = label; 
     label.text [email protected]"your title"; 
     [label sizeToFit]; 

self.title = @"My Title" 

这将更改导航栏标题以及引用此视图控制器的标题元素(例如,标签栏等)

self.navigationItem.title = @"My Title"; 

这将只改变当前视图控制器的导航栏的标题。