xcode 如何在不覆盖 drawRect 的情况下更改 uinavigationbar 背景颜色:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6902937/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:37:36  来源:igfitidea点击:

how to change the uinavigationbar background color without overriding drawRect:

iphoneobjective-cxcodeuinavigationcontrolleruinavigationbar

提问by TommyG

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.

我试图找到一种方法来为我的应用程序中的不同导航栏设置不同的背景颜色(我有几个视图,每个视图都有一个导航栏)。我不想覆盖 drawRect: 因为这适用于整个应用程序的全局。此外,使用 tintcolor 属性也无济于事,因为它仅适用于导航栏项目。

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

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

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

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

采纳答案by Jesse Naugher

subclass UINavigationBarand 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).

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

回答by Praveen-K

#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

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

回答by Praveen-K

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

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

回答by Joeran

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

is working for me, very easy way.

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

回答by rostam

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

您拥有的代码是正确的。你需要把它放在你创建导航控制器的地方。