如何更改 uinavigationbar 背景颜色而不覆盖drawRect:

发布于 2024-11-27 06:40:57 字数 327 浏览 0 评论 0原文

我正在尝试找到一种方法为我的应用程序中的不同导航栏设置不同的背景颜色(我有几个视图,每个视图都有一个导航栏)。我不打算覆盖drawRect:因为这适用于整个应用程序。此外,使用tintcolor属性没有帮助,因为它仅适用于导航栏项目。

self.navigationController.navigationBar.tintColor = [UIColor greenColor];

我已经搜索了好几天,找不到解决这个问题的直接方法。这么简单的事情怎么这么难?不明白为什么它不是 uinavbar 的属性...

请有人帮助提供一个简单的解决方案...谢谢!

I am trying to find a way to set a different background color for the different navigation bar in my app (i have a few views with a nav bar each). I am not looking to override drawRect: since this applies globally across the app. Also, using the tintcolor property doesnt help since it applies to the nav bar items only.

self.navigationController.navigationBar.tintColor = [UIColor greenColor];

I have been googling for days and cant find a straight solution to this problem. how come something so simple is so hard? dont understand why its not a property of the uinavbar...

someone please help with a simple solution...thanks!

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

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

发布评论

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

评论(5

柳若烟 2024-12-04 06:40:57
#import <QuartzCore/QuartzCore.h>

self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"navigationBarBackgroundImage"].CGImage;

self.navigationController.navigationBar.tintColor = [UIColor orangeColor];

正如您之前抱怨的那样,没有内容属性。你需要导入quartzframework

#import <QuartzCore/QuartzCore.h>

self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"navigationBarBackgroundImage"].CGImage;

self.navigationController.navigationBar.tintColor = [UIColor orangeColor];

as you were complaining before there is no contents properties. you need to import quartzframework

懒的傷心 2024-12-04 06:40:57

子类 UINavigationBar 并覆盖drawRect,然后仅在需要不同颜色的地方使用该子类。 (如果你想要很多不同的颜色,你可以有一个方法让你在初始化子类时传递颜色)。

subclass UINavigationBar and overwrite drawRect, then use the subclass only in places where you want the different color. (and you could have a method that lets you pass the color when you initialize the subclass or something if you want lots of different colors).

无声无音无过去 2024-12-04 06:40:57

当你说 self.navBar setTintColor:[UIColor greenColor]]; 时会发生什么在每个View的viewDidLoad中。其中 navBar 被声明为链接到界面生成器中导航栏的 IBOutlet UINavigationBar

what happens when you say self.navBar setTintColor:[UIColor greenColor]]; in viewDidLoad of eachView. where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder

夕嗳→ 2024-12-04 06:40:57
self.navigationController.navigationBar.backgroundColor = [UIColor greenColor];
self.navigationController.navigationBar.tintColor = [UIColor greenColor];

正在为我工​​作,非常简单的方法。

self.navigationController.navigationBar.backgroundColor = [UIColor greenColor];
self.navigationController.navigationBar.tintColor = [UIColor greenColor];

is working for me, very easy way.

仙女 2024-12-04 06:40:57

您拥有的代码是正确的。您需要将其放在创建导航控制器的位置。

The code you have is correct. You need to put it where you are creating the navigation controller.

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