2012-02-23 12 views
17

このコードは、アプリケーション内のどこでもUINavigationBarの色を変更することができます。しかし、私はUINavigationController(私はUIStoryboardから来る)UINavigationBarUIColorを変更しないことに気づいた。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

ありがとうございました!あなたはUINavigationControllerを宣言する場合

答えて

13

は、これを試してみてください。

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

注 - - 代わりに 'NSForegroundColorAttributeName'と' NSShadowAttributeName'を使用:iOSの8でwが、私たちは、このことにより、色合いの色を設定することができます。 – thomers

13

いいえ`UITextAttributeTextColor`と` UITextAttributeShadowColor`が廃止されていることを

self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 
関連する問題