首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改UINavigationItem颜色

更改UINavigationItem颜色
EN

Stack Overflow用户
提问于 2011-09-06 11:44:43
回答 3查看 6.1K关注 0票数 5

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

代码语言:javascript
复制
- (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)];

 }

加载应用程序时,一切看起来都很正常。离开视图并返回后,颜色将恢复为默认值。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-06 12:39:10

这里有一种方法:

代码语言:javascript
复制
[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

然而,巨大的警告。这很可能会在将来的操作系统版本中中断,因此不建议这样做。

至少,您应该执行大量测试,并确保您对导航栏的子视图布局的假设是正确的。也可以通过更改UINavigationBar的tintColor属性来更改颜色

代码语言:javascript
复制
myBar.tintColor = [UIColor greenColor];

或者您可以访问下面的链接http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/

票数 0
EN

Stack Overflow用户

发布于 2011-09-06 13:11:59

Change UINavigationItem colourto set same color to UISegmentControl将帮助您到达目的地。

是将颜色设置为UISegmentControl的示例代码。

票数 1
EN

Stack Overflow用户

发布于 2011-12-01 10:29:59

对于IOS5,必须使用"setBackgroundImage:forBarMetrics:"

尝试此代码以应用所有UINavigationItem / UINavigationBar

代码语言:javascript
复制
 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7314703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档