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

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

任何见解都将非常感激。


当前回答

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

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

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值

其他回答

现代的方法

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

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

你应该调用[label sizeToFit];设置文本后,防止奇怪偏移时,当其他按钮占据导航栏时,标签自动在标题视图中重新定位。

又短又甜。

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

亚历克斯·R·R的最新消息。的帖子使用新的iOS 7文本属性和现代objective c减少噪音:

NSShadow *titleShadow = [[NSShadow alloc] init];
titleShadow.shadowColor = [UIColor blackColor];
titleShadow.shadowOffset = CGSizeMake(-1, 0);
NSDictionary *navbarTitleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                            NSShadowAttributeName:titleShadow};

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

从ios5开始,我们必须使用titleTextAttribute字典(UInavigation控制器类引用中的预定义字典)设置标题文本的颜色和导航栏的字体。

 [[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor blackColor],UITextAttributeTextColor, 
[UIFont fontWithName:@"ArialMT" size:16.0], UITextAttributeFont,nil]];