我已经将我的第一个视图嵌入到了NavigationController中,并将segue设置为推送。 我想在每个视图中更改导航栏的标题以显示某些内容,并通过我的代码更新它。 我尝试像这样调用导航控制器:
self.navigationController.navigationBar.topItem.title = @"YourTitle";
但是这样不起作用,我该怎么做?
我已经将我的第一个视图嵌入到了NavigationController中,并将segue设置为推送。 我想在每个视图中更改导航栏的标题以显示某些内容,并通过我的代码更新它。 我尝试像这样调用导航控制器:
self.navigationController.navigationBar.topItem.title = @"YourTitle";
但是这样不起作用,我该怎么做?
导航控制器从视图控制器获取标题,因此这将完成工作
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Some Title";
}
self.navigationItem.title = @"YourTitle";
self.title = @"My Title"
self.navigationItem.title = @"My Title";
您也可以替换标题视图:
UIView* titleView = [[UIView alloc] init];
[self.navigationItem setTitleView: titleView];
self.title=@"your title";
但如果您想要为标题设置颜色或字体大小,则需要在其上放置一个UILabel。
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 =@"your title";
[label sizeToFit];