如何設定 tab icon 被選取時的顏色
Published in
2 min readJun 19, 2018
時常有人問到如何在 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