2014-02-12 47 views
-1

有人能告诉我如何在视图控制器中设置导航栏的颜色吗?Objective-c NavigationBar颜色变化?

我用这个:

UINavigationBar *navigationBar = self.navigationController.navigationBar; 

[navigationBar setBackgroundColor:[UIColor redColor]]; 

self.navigationController.navigationBar.tintColor = [UIColor redColor]; 

能有人能告诉我什么是tintColor以及如何从backgroundColor不同?

+0

该文档包含所有这些信息:https://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationBar_Class/Reference/UINavigationBar.html。查看barTintColor属性以更改导航栏背景颜色。 –

+0

“有人可以告诉我tintColor是什么,它与backgroundColor有什么不同”实际上,对于iOS 7,“backgroundColor”和“barTintColor”之间的区别应该是你应该担心的; 'tintColor'是无关的。 – matt

回答

0

要更改导航栏的颜色使用:

//Custom Navbar using image 
UIImage *navbar = [UIImage imageNamed:@"navbar.png"]; 
[[UINavigationBar appearance] setBackgroundImage:navbar forBarMetrics:UIBarMetricsDefault]; 

//Navbar color 
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; 

//Change the text color 
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIColor whiteColor],  NSForegroundColorAttributeName, nil]; 
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions]; 

来自http://ios-blog.co.uk/tutorials/ios-custom-ui-series-tabbar-navbar/由罗布·惠特洛

+0

OP不询问'UITabBar'。该问题明确指出OP有兴趣设置导航栏的颜色。 – CaptainRedmuff

+0

对不起,复制了错误的代码 – MarkP

1

更改导航栏的背景颜色很容易。例如,下一个代码将其更改为蓝色:

[ self.navigationController.navigationBar setBarTintColor : 
        [ UIColor colorWithRed : (0x0/255) 
             green : (0x0/255) 
             blue : (0xff/255) 
               alpha : 1 ] ]; 

您可以将以前的代码添加到您的viewDidLoad方法中。