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]];

希望这对你有所帮助。

谢谢

其他回答

这适用于我在Swift:

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

现代的方法

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

[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的回答。我的代码显示了效果,但他的代码提供了一种更简单的方法来将其放到现有视图控制器上。

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

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

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值

我知道这是一个相当老的话题,但我认为这对新用户来说是有用的,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];

对于iOS 7的使用,上面的大多数建议现在都已弃用

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
                               [UIColor whiteColor],NSForegroundColorAttributeName, 
                               [UIColor whiteColor],NSBackgroundColorAttributeName,nil];

self.navigationController.navigationBar.titleTextAttributes = textAttributes;
self.title = @"Title of the Page";

同样,签出NSAttributedString.h以获得可以设置的各种文本属性。