2014-02-12 122 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方法中。