Change color UINavigationItem

I need to set custom colors to my UINavigationBar buttons. I do the following (the RGB function is the definition):

- (void)viewWillAppear:(BOOL)animated
{

for (UIView *view in self.navigationController.navigationBar.subviews)      
    if ([[[view class] description] isEqualToString:@"UINavigationButton"])
        [(UINavigationButton *)view setTintColor:RGB(22.0,38.0,111.0)];

 }

Everything looks fine when the application loads. after exiting the view and returning the color, it returns to default.

Secondly, I need to configure the same color on the UISegmentedControl with the button I clicked.

+5
source share
3 answers

Here is one way:

[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

However, HUGE, leave. This is likely to lead to a break in a future version of the OS and is not recommended.

, , subview . , tintColor UINavigationBar

myBar.tintColor = [UIColor greenColor];

http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/

+1

[ UINavigationItem] (http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/ " UINavigationBarButtons" ) UISegmentControl, .

- UISegmentControl.

+1

For iOS5, you will have to use "setBackgroundImage: forBarMetrics:"

try this code to apply all UINavigationItem / UINavigationBar

 if([[UINavigationBar class] respondsToSelector:@selector(appearance)]){ //iOS >=5.0
    //use for UINavigationBar Custom image.
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithNamed:@"navImage.png"] forBarMetrics:UIBarMetricsDefault];

    //use for UINavigationItem custom color
    [[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
}
0
source

All Articles