2014-07-10 152 views
7

我已經設置了全局tintColor,並且我可以在界面生成器中看到它,當我選擇UITabBar和UITabBarController時,仍然在運行應用程序時,所選UITabBarItem的色調是iOS默認(藍色),而不是我設置的。我錯過了什麼?UITabBar tintColor沒有改變故事板

P.S. 的UITabBarController推到navigationController,它不是RootViewController的

回答

10

使用此代碼在您的appDelegate

[[UITabBar appearance] setSelectedImageTintColor: [UIColor redColor]]; 

didFinishLaunchingWithOptions:方法你想要的顏色替換紅色。

+0

很棒!謝謝。 – Dean

+0

我的榮幸@Dean!不推薦使用iOS setSelectedImageTintColor中的 –

+1

,應該使用tintColor代替。Swift版本: UITabBar.appearance()。tintColor = UIColor.red – Jurasic

4

如果你是針對iOS的8則

selectedImageTintColor在iOS 8的使用tintColor

斯威夫特

UITabBar.appearance().tintColor = UIColor.redColor() 

目標C棄用

[[UITabBar appearance] setTintColor: [UIColor redColor]]; 
+0

雖然unselectedItemTintColor工作正常,但我在appDelegate中放置了相同的代碼但不工作 – nikhil84

12

Storyboard不直接支持。但是您可以在故事板中設置用戶定義的運行時屬性

選擇標籤欄控制器的標籤欄。 Select the tab bar of the tab bar controller.

選擇身份檢查員。 (在這裏你可以設置類觀點的看法。)

Select the identity inspector

如果你想改變所選項目的着色顏色代替,只是用selectedImageTintColor關鍵路徑來代替。

+0

這是一個很好的解決方案,但我不確定爲什麼它不適用於iOS 10。由Scott工作的代碼解決方案很好 –

+0

由Scott工作的解決方案,但它有另一個問題,它改變了tabbar(tabbar上的所有項目)tintColor不是一個選項卡! –

+0

這是真正的贏家。結合我們從兩個答案中得到的結果:將關鍵路徑設置爲「selectedImageTintColor」而不是「tintColor」,並且不需要代碼。 – winfred

0

在我的應用程序中,我希望每個ViewController在呈現時都具有唯一的TabBarItem顏色。

enter image description here

在iOS系統8中,人工添加在故事板一個tintColor屬性的工作得很好,但不再有8.

我通過包括解決了這個問題的iOS下9/Xcode的任何影響在我的每個TabBarController的子ViewControllers中重寫每個ViewDidAppear()函數中的代碼。

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    self.tabBarController?.tabBar.tintColor = UIColor.whateverColor 
    //The rest of your code 
} 

這是在任何ViewController安全,由於?在tabBarController調用之後。如果ViewController未嵌入到TabBarController中,則整個行將被忽略。

通過將此代碼放置在每個VC中,您可以輕鬆地指定每個TabBarItem的顏色。