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

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

任何见解都将非常感激。


当前回答

我使用下面的代码为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]];

希望这对你有所帮助。

谢谢

其他回答

斯威夫特版本

我发现你们大多数人都给出了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

我已经自定义了导航栏的背景图像和左键项目,灰色标题不适合背景。然后我使用:

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

将淡色改为灰色。现在标题是白色的!这就是我想要的。

也希望能有所帮助:)

当我们在导航栏中插入一个按钮时,遇到了相同的问题(和其他人一样),标签移动(在我的情况下,我有一个旋转器,当日期被加载时,我用一个按钮替换),上述解决方案对我不起作用,所以这里是什么工作,并保持标签在同一位置一直:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
    // this will appear as the title in the navigation bar
    //CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
   CGRect frame = CGRectMake(0, 0, 180, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];



    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 yellowColor];
    self.navigationItem.titleView = label;
    label.text = NSLocalizedString(@"Latest Questions", @"");
    [label sizeToFit];
}

return self;

titleTextAttributes 显示栏标题文本的属性。

NSDictionary *titleTextAttributes (nonatomic, copy 讨论 你可以在文本属性字典中为标题指定字体、文本颜色、文本阴影颜色和文本阴影偏移量,使用NSString UIKit添加参考中描述的文本属性键。

可用性 支持iOS 5.0及更高版本。 中声明 UINavigationBar.h

方法一,设置为IB:

方法二,一行代码:

navigationController?.navigationBar.barTintColor = UIColor.blackColor()