2012-02-23 57 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]; 
相關問題