2012-02-23 55 views
17

此代码可以在应用程序中的任何地方更改UINavigationBar的颜色。然而,我注意到它并没有改变的UINavigationBar所使用的UINavigationController(我的来自UIStoryboard)。UINavigationController以全局方式和编程方式更改导航栏的色调颜色

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

谢谢!

回答

13

当你宣布你的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]; 
13

您可以更改条形颜色全球使用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]]; 
+1

注意'UITextAttributeTextColor'和'UITextAttributeShadowColor'已弃用 - 使用'NSForegroundColorAttributeName'和'NSShadowAttributeName'代替。 – thomers 2017-06-21 09:28:18

13

否w的的iOS 8我们可以通过此设置色调颜色: -

self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 
相关问题