Change UINavigationItem colour

梦想的初衷 提交于 2019-11-30 05:39:38

问题


I need to set custom colors to my UINavigationBar buttons. I'm doing the following thing(RGB func is a define):

- (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 on app load. after leaving the view and getting back the color returns to default.

Secondly I need to set the same colour to UISegmentedControl to a pressed button.


回答1:


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. or You can change the color by changing the tintColor property of the UINavigationBar

myBar.tintColor = [UIColor greenColor];

or You can follow the below link http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/




回答2:


[Change UINavigationItem colour](http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/"Changing colors of UINavigationBarButtons") and to set same color to UISegmentControl will help you to reach to your destination.

Here is a sample code for to set color to UISegmentControl.




回答3:


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


来源:https://stackoverflow.com/questions/7314703/change-uinavigationitem-colour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!