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

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

任何见解都将非常感激。


当前回答

我遇到了一个问题,我的导航按钮将文本抛出中心(当你只有一个按钮时)。为了解决这个问题,我改变了我的帧大小,如下所示:

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

其他回答

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

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

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

方法一,设置为IB:

方法二,一行代码:

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

现代的方法

现代的方法,对于整个导航控制器。,当你的导航控制器的根视图被加载时,做一次。

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

然而,这似乎对后续的视图没有影响。

经典的方法

旧的方法,每个视图控制器(这些常量适用于iOS 6,但如果你想在iOS 7的外观上实现每个视图控制器,你将需要相同的方法,但使用不同的常量):

你需要使用一个UILabel作为navigationItem的titleView。

标签应:

有一个明确的背景颜色(标签。backgroundColor = [UIColor clearColor])。 使用粗体20pt系统字体(标签。font = [UIFont boldSystemFontOfSize: 20.0f])。 有50% alpha的黑色阴影(标签。shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5])。 您还需要将文本对齐设置为居中(标签。textAlignment = NSTextAlignmentCenter(旧sdk的UITextAlignmentCenter)。

将标签文本颜色设置为您喜欢的任何自定义颜色。您确实希望使用一种不会导致文本混入阴影的颜色,否则很难阅读。

我通过反复试验得出了这个结论,但我最终得出的价值观太简单了,不可能不是苹果所选择的。:)

如果你想验证这一点,将这段代码放到PageThreeViewController中的initWithNibName:bundle:中。m的苹果导航条样本。这将用黄色标签替换文本。除了颜色之外,这应该与苹果代码生成的原始代码没有区别。

- (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
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = NSTextAlignmentCenter;
                           // ^-Use UITextAlignmentCenter for older SDKs.
        label.textColor = [UIColor yellowColor]; // change this color
        self.navigationItem.titleView = label;
        label.text = NSLocalizedString(@"PageThreeTitle", @"");
        [label sizeToFit];
    }

    return self;
}

编辑:还有,阅读下面埃里克B的回答。我的代码显示了效果,但他的代码提供了一种更简单的方法来将其放到现有视图控制器上。

又短又甜。

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

我知道这是一个相当老的话题,但我认为这对新用户来说是有用的,iOS 5带来了一个新的属性来建立标题属性。

你可以使用UINavigationBar的setTitleTextAttributes来设置字体、颜色、偏移量和阴影颜色。

此外,你可以为整个应用程序中的所有UINavigationBars设置相同的默认UINavigationBar的标题文本属性。

例如:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],UITextAttributeTextColor, 
                                            [UIColor blackColor], UITextAttributeTextShadowColor, 
                                            [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];