繁体   English   中英

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

[英]UINavigationController change navigation bar tint color globally and programmatically

此代码可以在应用程序内的任何地方更改UINavigationBar颜色。 但是,我注意到它并没有更改UINavigationBar使用的 UINavigationController UIColor (我的名字来自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's导航栏的appearance对象? 我知道如何设置各个控制器的色彩,但我想对其外观进行全局控制。

更新:这是我的错误,代码确实更改了所有UINavigationBarsUIColor ,但是它需要覆盖和发现根导航控制器(例如,提供一个模态视图控制器),然后它将使用新的UIColors重新绘制自身!

谢谢!

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

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

声明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];

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

完整的解决方案可立即更改导航栏的颜色并记住以后启动的偏好,可在以下位置找到: iPhone iOS如何按需重绘UINavigationBar?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM