更改 UINavigationItem 颜色

发布于 2024-12-03 03:03:51 字数 450 浏览 0 评论 0原文

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

- (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 相同的颜色。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时光的容颜 2024-12-10 03:03:51

[更改 UINavigationItem 颜色](http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/"更改 UINavigationBarButtons 的颜色") 和 为 UISegmentControl 设置相同的颜色将帮助您到达目的地。

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

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

‖放下 2024-12-10 03:03:51

这是一种方法:

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

但是,巨大的,警告。这很可能会在未来的操作系统版本中出现问题,因此不建议这样做。

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

myBar.tintColor = [UIColor greenColor];


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

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/

寂寞陪衬 2024-12-10 03:03:51

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

尝试此代码来应用所有 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]];
}

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]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文