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

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

UINavigationController change navigation bar tint color globally and programmatically

objective-cioscolorsuinavigationcontrollerstoryboard

提问by Alex Stone

This code can change color of a UINavigationBareverywhere within the application. However, I noticed that it does not change the UIColorof the UINavigationBarused byUINavigationController(mine comes from a UIStoryboard).

此代码可以更改UINavigationBar应用程序中任何地方的颜色。然而,我注意到,它并没有改变UIColor的的UINavigationBar所用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];

Is there a way to access the appearanceobject of a UINavigationController'snavigation bar?I know how to set tints of individual controllers, but I want to have a global control over how they look.

有没有办法访问导航栏的appearance对象UINavigationController's我知道如何设置单个控制器的色调,但我想对它们的外观进行全局控制。

Update: This was my mistake, the code does change the UIColorof 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!

更新:这是我的错误,代码确实改变了UIColor所有的UINavigationBars,但它需要覆盖和揭露根导航控制器(例如呈现一个模态视图控制器),然后它会用 new 重新绘制自己UIColors

Thank you!

谢谢!

采纳答案by Alex Stone

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?

可以在此处找到立即更改导航栏颜色并记住后续启动首选项的完整解决方案: iPhone iOS 如何按需重绘 UINavigationBar?

回答by Ashish

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

现在在 iOS 8 中,我们可以通过以下方式设置色调颜色:-

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

回答by Nilesh

You can change bar colours globally using appearanceproxy:

您可以使用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]];

回答by demon9733

When you declaring your UINavigationController, try this:

当你声明你的时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];