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

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

任何见解都将非常感激。


当前回答

Swift 4和4.2版本:

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

其他回答

现代的方法

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

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

我知道这是一个相当老的话题,但我认为这对新用户来说是有用的,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以获得可以设置的各种文本属性。

self.navigationItem.title=@"Extras";
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue" size:21], NSFontAttributeName,[UIColor whiteColor],UITextAttributeTextColor,nil]];

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

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