I would like to have control over the color of a TabBar item based on some event.
I have tried the following:
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for:.selected)
UITabBar.appearance().backgroundColor = UIColor(red:1, green:0, blue:0, alpha:1) /
UITabBar.appearance().tintColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1) // New!!
After much experimenting, the only way I have been able to trigger this code is to place them within
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{...}
I have also created a TabBarController.swift file and linked that to the tab bar view controller. Within this Swift file I have used the following function:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("\nIn > MyTabBarViewController > tabBarController() ..................................9-9-9-9-9\n\n")
if viewController is Tab1ViewController {
print("Tab One Pressed")
} else if viewController is Tab2ViewController {
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for:.selected)
UITabBar.appearance().backgroundColor = UIColor(red:1, green:0, blue:0, alpha:1) /
UITabBar.appearane().tintColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1) /
print("Tab Two Perssed")
}
Pressing either tab bar item does result in the printed text appearing in the console but the code within the if-stmt is ignored.
Any help will be greatly appreciated. Thanks.