After update to iOS 15, I implemented UITabBar configuration this way:
let backgroundColor = UIColor.grey
let selectedItemTextColor = UIColor.blue
let unselectedItemTextColor = UIColor.black
if #available(iOS 15, *) {
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.backgroundColor = backgroundColor
tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selectedItemTextColor]
tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unselectedItemTextColor]
tabBar.standardAppearance = tabBarAppearance
tabBar.scrollEdgeAppearance = tabBarAppearance
} else {
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: selectedItemTextColor], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: unselectedItemTextColor], for: .normal)
tabBar.barTintColor = backgroundColor
}
This works fine for iOS 15 and older versions.
But in my project I need to set selected/unselected text color for one of tab bar items, different from other items. Set it in runtime.
There are 5 tab bar items. In some moment, I need this behavior: four of them should have blue/black text color (for selected/unselected states) and one should have red/green color.
Until iOS 15, I used this code to set colors of needed item in every moment of time:
let indexOfItemToChange = 4
tabBar.items[indexOfItemToChange].setTitleTextAttributes([.foregroundColor: UIColor.red], for: .selected)
tabBar.items[indexOfItemToChange].setTitleTextAttributes([.foregroundColor: UIColor.green], for: .normal)
After update update to iOS 15 it makes no effect. I have tried to set it like this:
let indexOfItemToChange = 4
tabBar.items[indexOfItemToChange].standardAppearance?.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.green]
tabBar.items[indexOfItemToChange].standardAppearance?.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor.red]
But it makes no effect too. Even if before setting of tab bar item colors I set UITabBar appearances for each tab bar item:
tabBar.items.forEach {
$0.standardAppearance = standardAppearance
$0.scrollEdgeAppearance = scrollEdgeAppearance
}
Some one faced this issue? Any advice?
PS. In my project there are no xibs and storyboards. So I need to solve my problem only programmatically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.