views:

4785

answers:

7

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?

+1  A: 

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

Steven Fisher
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
Oh I take that back - it does work. See follow up answer...
schwa
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
And you can also code defensively to make sure you dont go boom. But still - I'd prefer a real API. ;-)
schwa
+2  A: 

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.

schwa
I'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
Right. I didn't post the actual way to do it. Just the first method that worked.Defensive programming ftw.
schwa
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
+2  A: 

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

+2  A: 

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.

Ross Boucher
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
A: 

Hi,

I want to change the width of the navigation default button. I have a big Title for my navigation bar but i want my default back button to be small. how do i do it??

Thanks in advance.

werrtrt
A: 

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

yonel
This doesn't affect the color of the navigation item's left and right buttons.
lawrence
-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
+6  A: 
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];
    }

}
Slim