UINavigationController 以编程方式全局更改导航栏色调颜色

发布于 2025-01-08 19:18:02 字数 943 浏览 3 评论 0原文

此代码可以更改应用程序中各处 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对象?我知道如何设置各个控制器的色调,但我想对他们的外观有全局的控制权。

更新: 这是我的错误,代码确实更改了所有 UINavigationBarsUIColor,但它需要覆盖和暴露根导航控制器(例如呈现模式视图控制器) ,然后它将用新的 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 技术交流群。

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

发布评论

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

评论(4

酷到爆炸 2025-01-15 19:18:02

现在在 iOS 8 中我们可以这样设置色调颜色:-

self.navigationController.navigationBar.barTintColor = [UIColor redColor];

Now in iOS 8 we can set tint color by this :-

self.navigationController.navigationBar.barTintColor = [UIColor redColor];
走走停停 2025-01-15 19:18:02

您可以使用 appearance 代理全局更改条形颜色:

NSDictionary *textTitleOptions = 
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], 
                                           UITextAttributeTextColor, 
                                           [UIColor whiteColor],  
                                           UITextAttributeTextShadowColor, nil];

[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
textTitleOptions = 
 [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], 
                                            UITextAttributeTextColor, nil];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UIToolbar appearance] setTintColor:[UIColor redColor]];
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];

You can change bar colours globally using appearance proxy:

NSDictionary *textTitleOptions = 
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], 
                                           UITextAttributeTextColor, 
                                           [UIColor whiteColor],  
                                           UITextAttributeTextShadowColor, nil];

[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
textTitleOptions = 
 [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], 
                                            UITextAttributeTextColor, nil];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UIToolbar appearance] setTintColor:[UIColor redColor]];
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
つ低調成傷 2025-01-15 19:18:02

当您声明 UINavigationController 时,请尝试以下操作:

UINavigationController *myNavController = 
[[UINavigationController alloc] initWithRootViewController:myRootViewController];
myNavController.navigationBar.tintColor = 
    [UIColor colorWithRed:arc4random() % 100 / 100.0f
                    green:arc4random() % 100 / 100.0f
                     blue:arc4random() % 100 / 100.0f
                    alpha:1.0f];

When you declaring your UINavigationController, try this:

UINavigationController *myNavController = 
[[UINavigationController alloc] initWithRootViewController:myRootViewController];
myNavController.navigationBar.tintColor = 
    [UIColor colorWithRed:arc4random() % 100 / 100.0f
                    green:arc4random() % 100 / 100.0f
                     blue:arc4random() % 100 / 100.0f
                    alpha:1.0f];
爱她像谁 2025-01-15 19:18:02

可以在此处找到立即更改导航栏颜色并记住后续启动的首选项的完整解决方案:
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?

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