如何更改 uinavigationbar 背景颜色而不覆盖drawRect:
我正在尝试找到一种方法为我的应用程序中的不同导航栏设置不同的背景颜色(我有几个视图,每个视图都有一个导航栏)。我不打算覆盖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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如您之前抱怨的那样,没有内容属性。你需要导入quartzframework
as you were complaining before there is no contents properties. you need to import quartzframework
子类
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).当你说 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
正在为我工作,非常简单的方法。
is working for me, very easy way.
您拥有的代码是正确的。您需要将其放在创建导航控制器的位置。
The code you have is correct. You need to put it where you are creating the navigation controller.