如何設定 tab icon 被選取時的顏色

時常有人問到如何在 storyboard 設定 tab icon 被選取時的顏色,比方如上圖,Favorites tab 的圖片和文字顏色為橘色。

其實答案很簡單,設定 Tab Bar 的 Image Tint 欄位,即可控制被選取 tab 的圖片和文字顏色。

如果想從程式設定也可以,有以下幾個方法

方法1:
設定 tab bar 的 tintColor

tabBarController?.tabBar.tintColor = UIColor.red

方法2:
設定 window 的 tintColor。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {   window?.tintColor = UIColor.red   return true}

方法3:
設定 tab bar 的 UIAppearance 的 tintColor。

UITabBar.appearance().tintColor = UIColor.red

--

--

彼得潘的 iOS App Neverland
彼得潘的 Swift iOS App 開發問題解答集

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com