UINavigationController 以编程方式全局更改导航栏色调颜色
此代码可以更改应用程序中各处 UINavigationBar
的颜色。但是,我注意到它不会改变 UINavigationBar
使用的 UIColor
UINavigationController
(我的来自UIStoryboard
)。
UIColor* navBarColor = [UIColor colorWithRed:arc4random()%100/100.0
green:arc4random()%100/100.0
blue:arc4random()%100/100.0
alpha:1];
[[UINavigationBar appearance] setTintColor:navBarColor];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setAlpha:0.7];
有没有办法访问UINavigationController的
导航栏的appearance
对象?我知道如何设置各个控制器的色调,但我想对他们的外观有全局的控制权。
更新: 这是我的错误,代码确实更改了所有 UINavigationBars
的 UIColor
,但它需要覆盖和暴露根导航控制器(例如呈现模式视图控制器) ,然后它将用新的 UIColors
重新绘制自己!
谢谢你!
This code can change color of a UINavigationBar
everywhere within the application. However, I noticed that it does not change the UIColor
of the UINavigationBar
used by UINavigationController
(mine comes from a UIStoryboard
).
UIColor* navBarColor = [UIColor colorWithRed:arc4random()%100/100.0
green:arc4random()%100/100.0
blue:arc4random()%100/100.0
alpha:1];
[[UINavigationBar appearance] setTintColor:navBarColor];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setAlpha:0.7];
Is there a way to access the appearance
object of a UINavigationController's
navigation bar? I know how to set tints of individual controllers, but I want to have a global control over how they look.
Update:
This was my mistake, the code does change the UIColor
of all UINavigationBars
, but it requires the root navigation controller to be covered and uncovered(for example presenting a modal view controller), then it will re-draw itself with new UIColors
!
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现在在 iOS 8 中我们可以这样设置色调颜色:-
Now in iOS 8 we can set tint color by this :-
您可以使用
appearance
代理全局更改条形颜色:You can change bar colours globally using
appearance
proxy:当您声明
UINavigationController
时,请尝试以下操作:When you declaring your
UINavigationController
, try this:可以在此处找到立即更改导航栏颜色并记住后续启动的首选项的完整解决方案:
iPhone iOS 如何按需重绘 UINavigationBar?
The complete solution which changes the navbar color instantly and remembers the preference for subsequent launches can be found here:
iPhone iOS how to redraw UINavigationBar on demand?