iPhone Navigation Bar Title text color

2019-01-01 11:35发布

It seems the iOS Navigation Bar title color is white by default. Is there a way to change it to a different color?

I am aware of the navigationItem.titleView approach using an image. Since my design skills are limited and I failed to get the standard glossy, I prefer changing the text color.

Any insight would be much appreciated.

30条回答
长期被迫恋爱
2楼-- · 2019-01-01 12:02

Use the code below in any view controller viewDidLoad or viewWillAppear method.

- (void)viewDidLoad
{
    [super viewDidLoad];

    //I am using UIColor yellowColor for an example but you can use whatever color you like   
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};

    //change the title here to whatever you like
    self.title = @"Home";
    // Do any additional setup after loading the view.
}
查看更多
看风景的人
3楼-- · 2019-01-01 12:03

This is one of those things that are missing. Your best bet is to create your own custom Navigation Bar, add a text box, and manipulate the color that way.

查看更多
柔情千种
4楼-- · 2019-01-01 12:04

I've customized the navigationBar's background image and left button item, and the gray title not fit the background. Then I use:

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

to change the tint color to gray. And the title is white now! That's what I want.

Hope to help also :)

查看更多
伤终究还是伤i
5楼-- · 2019-01-01 12:04

titleTextAttributes Display attributes for the bar’s title text.

@property(nonatomic, copy) NSDictionary *titleTextAttributes Discussion You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary, using the text attribute keys described in NSString UIKit Additions Reference.

Availability Available in iOS 5.0 and later. Declared In UINavigationBar.h

查看更多
一个人的天荒地老
6楼-- · 2019-01-01 12:06

I ran into the problem with my nav buttons throwing the text out of center (when you only have one button). To fix that I just changed my frame size like so:

CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
查看更多
人间绝色
7楼-- · 2019-01-01 12:07

In IOS 7 and 8, you can change the Title's color to let's say green

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor greenColor] forKey:NSForegroundColorAttributeName];
查看更多
登录 后发表回答