更改UINavigationItem颜色

问题描述:

我需要为我的UINavigationBar按钮设置自定义颜色。
我正在做以下事情(RGB func是一个定义):

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.

其次,我需要将相同的颜色设置为UISegmentedControl到按下的按钮。

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

以下是一种方式:

[[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.

至少你应该进行大量的测试,并确保你的假设是导航栏的子视图布局是正确的。

您可以通过更改UINavigationBar的tintColor属性来更改颜色

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


您可以点击以下链接
http://www.skylarcantu.com/blog/2009/11/05/改变颜色的uinavigationbarbuttons /