2012-02-23 2 views
17

Этот код может изменять цвет UINavigationBar везде в приложении. Тем не менее, я заметил, что не изменяет UIColorUINavigationBar, используемый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]; 

Есть ли способ, чтобы получить доступ к appearance объекту UINavigationController's панели навигации в? Я знаю, как устанавливать оттенки отдельных контроллеров, но я хочу иметь глобальный контроль над тем, как они выглядят.

Update: Это была моя ошибка, код действительно изменить UIColor всех UINavigationBars, но требует контроллер корневого навигационный быть покрытые и непокрытые (например, представляющий модальный контроллер представления), то он будет перерисовать сам с новым 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

13

Нет ж в прошивке 8 мы можем установить оттенок цвета этим: -

self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 
Смежные вопросы