iPhone导航栏标题文字颜色

289

默认情况下,iOS导航栏标题颜色为白色。是否有方法将其更改为其他颜色?

我知道可以使用navigationItem.titleView方法使用图像的方式进行更改。由于我的设计技能有限并且我未能获得标准的光泽效果,因此我更喜欢更改文本颜色。

非常感谢您的任何见解。


我刚发布了一段代码,基于Steven Fisher的答案,简化了添加自定义彩色标题到导航栏的过程。它还支持更改标题。去找找吧!它不会让你失望的。 - Erik B
1
Erik: 我已经在我的答案中加了一条关于你的回答的注释。我可以用你的代码更新我的答案,但我不想获得你的点赞。顺便说一句,聪明的解决方案。 - Steven Fisher
针对IOS 13 https://dev59.com/3bbna4cB1Zd3GeqPcYqd#61141195 - Vladyslav Panchenko
32个回答

13

iOS 5及以上版本中,我们需要使用titleTextAttribute字典(UInavigation控制器类参考中的预定义字典)来设置导航栏的标题文本颜色和字体。

 [[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor blackColor],UITextAttributeTextColor, 
[UIFont fontWithName:@"ArialMT" size:16.0], UITextAttributeFont,nil]];

10

在任何视图控制器的viewDidLoad或viewWillAppear方法中使用以下代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //I am using UIColor yellowColor for an example but you can use whatever color you like   
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};

    //change the title here to whatever you like
    self.title = @"Home";
    // Do any additional setup after loading the view.
}

10

简短而精炼。

[[[self navigationController] navigationBar] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];

8

这是基于Stevens的解决方案。

唯一的区别是我加入了一些处理来根据文本长度调整位置,似乎类似于苹果的做法。

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft), 0, 480,44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont boldSystemFontOfSize: 20.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleLabel.textAlignment = ([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft);
titleLabel.textColor = [UIColor redColor];
titleLabel.text = self.title;
self.navigationItem.titleView = titleLabel;
[titleLabel release];

您可能需要根据字体大小调整10的值。


7

Swift 4和4.2版本:

 self.navigationController.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]

6

我遇到了导航按钮的问题,当只有一个按钮时,文本会偏离中心。为了解决这个问题,我只需要像下面这样更改框架大小:

CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);

6

我已经自定义了navigationBar的背景图片和左侧按钮,但是灰色标题和背景不匹配。然后我使用如下代码:

[self.navigationBar setTintColor:[UIColor darkGrayColor]];

把色调颜色变成灰色。现在标题是白色的!这就是我想要的。

希望也能帮到你 :)


不错,但这只适用于将文本更改为白色。即使使用[UIColor whiteColor]对navBar进行着色也会将文本颜色更改为白色。 - cschuff

6

这是一个相当古老的帖子,但我想提供答案,以设置iOS 7及以上版本导航栏标题的颜色、大小和垂直位置。

关于颜色和大小

 NSDictionary *titleAttributes =@{
                                NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
                                NSForegroundColorAttributeName : [UIColor whiteColor]
                                };

竖直位置

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-10.0 forBarMetrics:UIBarMetricsDefault];

设置标题并分配属性字典

[[self navigationItem] setTitle:@"CLUBHOUSE"];
self.navigationController.navigationBar.titleTextAttributes = titleAttributes;

6

建议设置self.title,因为在推送子导航栏或在选项卡栏上显示标题时会使用它。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // create and customize title view
        self.title = NSLocalizedString(@"My Custom Title", @"");
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        titleLabel.text = self.title;
        titleLabel.font = [UIFont boldSystemFontOfSize:16];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        [titleLabel sizeToFit];
        self.navigationItem.titleView = titleLabel;
        [titleLabel release];
    }
}

5

下面的代码适用于Swift:

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]

非常接近,但问题在于您的方法将覆盖标题上已有的所有可能属性。在我的情况下,那是字体。我最终使用的代码是navigationController?.navigationBar.titleTextAttributes = { 如果当前属性存在,则为当前属性 var newAttributes = currentAttributes newAttributes [NSForegroundColorAttributeName] = navigationTintColor return newAttributes } else {return [NSForegroundColorAttributeName:navigationTintColor]}}() - Matic Oblak
运行良好。Obj-C 也是一样的:self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : UIColor.redColor}; - Mike Keskinov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接