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

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

任何见解都将非常感激。


当前回答

根据Steven Fisher的回答,我写了这样一段代码:

- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];

        titleView.textColor = [UIColor yellowColor]; // Change to desired color

        self.navigationItem.titleView = titleView;
        [titleView release];
    }
    titleView.text = title;
    [titleView sizeToFit];
}

这段代码的优点,除了正确地处理帧,是如果你改变你的控制器的标题,自定义标题视图也会得到更新。无需手动更新。

另一个很大的优势是,它使得启用自定义标题颜色非常简单。您所需要做的就是将这个方法添加到控制器中。

其他回答

建议设置self。标题,因为这是用在推子导航栏或显示标签栏标题。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // create and customize title view
        self.title = NSLocalizedString(@"My Custom Title", @"");
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        titleLabel.text = self.title;
        titleLabel.font = [UIFont boldSystemFontOfSize:16];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        [titleLabel sizeToFit];
        self.navigationItem.titleView = titleLabel;
        [titleLabel release];
    }
}

我相信设置UINavigationBar颜色的正确方法是:

NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor, nil];
self.titleTextAttributes = attributes;

上面写的代码是UINavigationBar上的子类,显然没有子类也可以工作。

Swift 4和4.2版本:

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

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

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

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

也希望能有所帮助:)

现代的方法

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

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