Lawrence
Lawrence

Reputation: 10780

How to change the text color on UINavigationBar button items

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?

Upvotes: 19

Views: 26673

Answers (8)

Bhavin_m
Bhavin_m

Reputation: 2784

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]; 

Upvotes: -1

Michael
Michael

Reputation: 1355

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:

Upvotes: 31

Taras Kalapun
Taras Kalapun

Reputation: 1761

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];
    }

}

Upvotes: 11

yonel
yonel

Reputation: 7865

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];

Upvotes: 0

Ross Boucher
Ross Boucher

Reputation: 702

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.

Upvotes: 2

Drunknbass
Drunknbass

Reputation:

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

Upvotes: 2

schwa
schwa

Reputation: 11942

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.

Upvotes: 8

Steven Fisher
Steven Fisher

Reputation: 44886

Can't you iterate the contents of the UINavigationBar view to find the buttons in the awakeFromNib?

Upvotes: 1

Related Questions