xcode 更改 UINavigationItem 颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7314703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change UINavigationItem colour
提问by Misha
I need to set custom colors to my UINavigationBar buttons. I'm doing the following thing(RGB func is a define):
我需要为我的 UINavigationBar 按钮设置自定义颜色。我正在做以下事情(RGB func 是一个定义):
- (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.
其次,我需要将 UISegmentedControl 的相同颜色设置为按下的按钮。
采纳答案by Nikunj Jadav
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
至少您应该执行大量测试并确保您对导航栏子视图布局的假设是正确的。或者您可以通过更改 UINavigationBar 的 tintColor 属性来更改颜色
myBar.tintColor = [UIColor greenColor];
or You can follow the below link http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/
或者您可以点击以下链接 http://www.skylarcantu.com/blog/2009/11/05/changed-colors-of-uinavigationbarbuttons/
回答by alloc_iNit
[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 UISegmentControlwill help you to reach to your destination.
[更改 UINavigationItem 颜色](http://www.skylarcantu.com/blog/2009/11/05/changed-colors-of-uinavigationbarbuttons/"更改 UINavigationBarButtons 的颜色") 并将相同的颜色设置为 UISegmentControl将帮助您到达目的地。
Hereis a sample code for to set color to UISegmentControl.
这是一个用于为 UISegmentControl 设置颜色的示例代码。
回答by HelmiB
For IOS5 will have to use "setBackgroundImage:forBarMetrics:"
对于 IOS5 将不得不使用“setBackgroundImage:forBarMetrics:”
try this code to apply all UINavigationItem / UINavigationBar
尝试使用此代码应用所有 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]];
}