iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?

我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。

任何见解都将非常感激。


当前回答

这是一个相当老的线程,但我认为提供的答案设置的颜色,大小和垂直位置的导航栏标题iOS 7及以上

颜色和尺寸

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

垂直位置

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

设置Title并分配属性字典

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

其他回答

这是我基于史蒂文斯的解决方案

唯一真正的区别是,如果根据文本长度,我将一些处理用于调整位置,似乎与苹果的做法类似

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值

斯威夫特版本

我发现你们大多数人都给出了Objective_C版本的答案

我想通过使用Swift为任何需要它的人实现这个功能。

在ViewDidload

1.要使导航栏背景变成颜色(例如:蓝色)

self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()

2.要使导航栏背景变成图像(例如:ABC.png)

let barMetrix = UIBarMetrics(rawValue: 0)!

self.navigationController?.navigationBar
      .setBackgroundImage(UIImage(named: "ABC"), forBarMetrics: barMetrix)

3.更改导航栏标题(例如:[字体:Futura,10][颜色:红色])

navigationController?.navigationBar.titleTextAttributes = [
            NSForegroundColorAttributeName : UIColor.redColor(),
            NSFontAttributeName : UIFont(name: "Futura", size: 10)!
        ]

(提示1:不要忘记UIFont后面的“!”标记) (hint2:有很多属性的标题文本,命令点击 在“NSFontAttributeName”中,你可以进入类并查看keyNames 和他们需要的对象类型)

希望我能帮上忙!: D

Swift 4和4.2版本:

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

这是我们缺失的东西之一。您最好的办法是创建自己的自定义导航栏,添加一个文本框,并以这种方式操作颜色。

我使用下面的代码为iOS 9和它的工作对我来说很好。我也使用阴影颜色的标题。

self.navigationItem.title = @"MY NAV TITLE";
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                       shadow, NSShadowAttributeName,
                                                       [UIFont fontWithName:@"HelveticaNeue-Light" size:21.0], NSFontAttributeName, nil]];

希望这对你有所帮助。

谢谢