2012-02-23 17 views
17

Ten kod może zmienić kolor UINavigationBar wszędzie w obrębie aplikacji. Jednak zauważyłem, że to nie zmienia UIColor z UINavigationBar używany przezUINavigationController (mój pochodzi od UIStoryboard).UINavigationController zmienia kolor odcienia paska nawigacji globalnie i programowo

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

Czy istnieje sposób, aby uzyskać dostęp do appearance przedmiot barze UINavigationController's nawigacji? Wiem, jak ustawić odcienie poszczególnych kontrolerów, ale chcę mieć globalną kontrolę nad tym, jak wyglądają.

Aktualizacja: To był mój błąd, kod nie zmienia UIColor wszystkich UINavigationBars, ale wymaga kontrolera nawigacyjnego korzeń być kryty i odkryty (na przykład prezentując modalne kontroler widoku), a następnie będzie ponownie remis sam z nowym UIColors!

Dziękujemy!

Odpowiedz

13

Kiedy deklarując swoją UINavigationController, spróbuj tego:

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

Można zmienić kolory słupków na całym świecie przy użyciu appearance proxy:

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

Zauważ, że '' UITextAttributeShadowColor' UITextAttributeTextColor' i były przestarzałe - użyć 'NSForegroundColorAttributeName' i' NSShadowAttributeName' zamiast. – thomers

13

Nie W w iOS 8 możemy ustawić kolor odcień przez to: -

self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 
Powiązane problemy