更改在故事板中拖动的导航栏的标题

时间:2014-01-26 21:47:36

标签: ios objective-c uinavigationbar

我想更改导航栏标题的字体和颜色,我从调色板中拖动到故事板中(不是使用nag控制器+推送segue自动创建的)。 我已经尝试了

self.navigationController.topViewController.title = @"title";
self.title = @"title";
self.navigationItem.title = @"title";

但它们都不起作用。没有标题显示,所以我甚至没有尝试任何字体或颜色的更改。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

确保导航栏是某个navigationController的一部分。

试试这个:

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

[self.navigationController.navigationBar setBackgroundImage: [UIImage imageNamed:@"NavBarColor"] forBarMetrics: UIBarMetricsDefault];

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];

self.title = @"Title here!";

否则:

 UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 170.0f, 44.0f)];

    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment   = NSTextAlignmentCenter;
    titleLabel.textColor       = [UIColor colorWithRed: 0.922f green: 0.925f blue: 0.973f alpha: 1.0f];
    titleLabel.font            = [Utils helveticaBoldFontOfSize: 19.333f];
    titleLabel.text            = NSLocalizedString(@"Title Here", nil);
    self.navigationItem.titleView = titleLabel;