I've got a UINavigationController and i've changed it to white using the Tint property of the navigation bar in Interface Builder. But the text in buttons and the title is still the default color, white, and so gets lost against the white background. Anyone know how to work around this?
8 Answers
As of iOS 5 this it much easier, using the UIBarButtonItem appearance proxy:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor],
UITextAttributeTextColor,
[UIColor clearColor],
UITextAttributeTextShadowColor, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
forState: UIControlStateNormal];
Set this in application:didFinishLaunchingWithOptions:

- 1,355
- 2
- 12
- 15
-
1This could be applied to individual UIBarButtonItem(s) as well. For an actual button of the bar you could write: [button setTitleTextAttributes:textAttributes forState:UIControlStateNormal]; – Don Miguel Jul 03 '14 at 10:07
for (id subView in theNavigationBar.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
[(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[(UIButton *)subView setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
}
}

- 1,761
- 15
- 19
Here's one way:
[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
However, HUGE, caveat. This is highly likely to break on a future OS release and is not recommended.
At the very least you should perform a lot of testing and make sure you your assumptions of the subview layout of the navigation bar are correct.

- 11,962
- 14
- 43
- 54
-
1I'd find that in particular fragile, since you're accessing the items by index. More safe would be to get all the subview items, compare classes against what you expect, and even use positions. But that's the kernel of the idea right there. :) – Steven Fisher Mar 26 '09 at 17:53
-
Right. I didn't post the actual way to do it. Just the first method that worked. Defensive programming ftw. – schwa Mar 26 '09 at 18:33
-
how funny that i don't get an answer for almost two months, and then i get a whole crop of them... anyway, i marked this one the answer because it's the most scalable, even though what i ended up using custom button images. hopefully something makes it into the official API by v3.0. – Lawrence Mar 31 '09 at 05:46
Or you use your own button bar item subclass with a setter you specify, lus isn't iPhone os 3 suppose to exposé text color for EVERY button
I did as drunknbass suggested. I resorted to making a series of images for back-button in a few states, and regular buttons in a few states, and put them in a custom UIButton subclass, setting up the appropriate styles.
As a solution it doesn't scale particularly well, but my needs were simple enough. It has the advantage of not breaking if the subview orders in the built in controls change though, which is the obvious downside of that approach. Disadvantage of needing several images. The other tricky thing is handling an orientation change, since the buttons should change size.
On a related note, if you do change the tint color of your navigation bar, it does not play well with the special "More" view controller for customizing the order of a tab bar. There doesn't seem to be any "acceptable" way to change the color of the toolbar in that view.

- 704
- 2
- 6
- 10
-
That's a fine solution for a single nav bar. But as you admitted it doesn't scale well at all. I'd probably use a solution like this normally. But I'm working on an app that has very dynamic ui requirements. I might be forced to do something more hacky :-( – schwa Mar 26 '09 at 03:02
Can't you iterate the contents of the UINavigationBar view to find the buttons in the awakeFromNib?

- 44,462
- 20
- 138
- 192
-
You can. But I'm doing a setTextColor:forStates: on my buttons and it seems to be ignored. Will persevere though. Also that borders on private api (or at least private assumed knowledge of structure of UI) usage. – schwa Mar 26 '09 at 02:40
-
I've peeked inside other views like this. While it's a bit private API-ish, it at least makes no assumptions: if the internals change, your code won't detect whatever condition you're searching for. But I haven't done UINavigationBar yet. – Steven Fisher Mar 26 '09 at 02:42
-
And you can also code defensively to make sure you dont go boom. But still - I'd prefer a real API. ;-) – schwa Mar 26 '09 at 02:50
Here the clean way (relying on public API) to do this IMHO : you should use the titleView
property of the navigationItem
to apply it your own UILabel
on which you will customize the textColor
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
lbl.textAlignment=UITextAlignmentCenter;
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor=[UIColor colorWithRed:0.18 green:0.2 blue:0.56 alpha:1];
theControllerOnWhichYouWantToHaveATitleWithYourOwnColor.navigationItem.titleView=lbl;
[lbl release];

- 7,855
- 2
- 44
- 51
-
This doesn't affect the color of the navigation item's left and right buttons. – Lawrence Mar 15 '10 at 17:22
-
-1 ! :) Ok this does not solve the back button for instance, but it solves the issue for the main title view. The same approach could be applied to the back button on which you can set a custom view as well. Then I agree you have to build the correct view with the correct background image and so on, but this is "feasible", and with the public API. – yonel Mar 15 '10 at 18:35
-
Of course you'll get downvoted if you answer a different question. Your answer would be ok for this question: http://stackoverflow.com/questions/599405/iphone-navigation-bar-title-text-color – Erik B May 05 '11 at 15:25
you can change title appearance by adding label into the navigation bar like bellow.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 35)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = viewLabel;
[label release];

- 2,746
- 3
- 30
- 49