iPhone导航栏标题文字颜色

289

默认情况下,iOS导航栏标题颜色为白色。是否有方法将其更改为其他颜色?

我知道可以使用navigationItem.titleView方法使用图像的方式进行更改。由于我的设计技能有限并且我未能获得标准的光泽效果,因此我更喜欢更改文本颜色。

非常感谢您的任何见解。


我刚发布了一段代码,基于Steven Fisher的答案,简化了添加自定义彩色标题到导航栏的过程。它还支持更改标题。去找找吧!它不会让你失望的。 - Erik B
1
Erik: 我已经在我的答案中加了一条关于你的回答的注释。我可以用你的代码更新我的答案,但我不想获得你的点赞。顺便说一句,聪明的解决方案。 - Steven Fisher
针对IOS 13 https://dev59.com/3bbna4cB1Zd3GeqPcYqd#61141195 - Vladyslav Panchenko
32个回答

0

我正在使用以下代码适用于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]];

希望这能对你有所帮助。

谢谢。


只需要将 navigationBar.barStyle 设置为 blackStyle,标题就会变成白色。 - zszen

0
为了使Erik B的优秀解决方案在您应用程序的不同UIVIewCOntroller中更易于使用,我建议为UIViewController添加一个类别,并在其中声明他的setTitle:title方法。这样,您将获得所有视图控制器上的标题颜色更改,而无需重复。需要注意的一点是,在Erik的代码中,您不需要[super setTItle:tilte];,并且您需要在视图控制器中显式调用self.title =“my new title”才能调用此方法。
@implementation UIViewController (CustomeTitleColor)

- (void)setTitle:(NSString *)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 blueColor]; // Change to desired color

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

@end


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接