Setting of different text colors for different UITabBarItem's in iOS 15

Viewed 1552

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.

1 Answers

We can use this code to change TabBar item appearance:-

  if #available(iOS 15.0, *) {
        
        let tabBarAppearance = UITabBarAppearance()
        let tabBarItemAppearance = UITabBarItemAppearance()
        
        tabBarAppearance.backgroundColor = .white
        
        tabBarItemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: Constants.Color.appDefaultBlue]
        tabBarItemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
        
        tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
        tabBar.standardAppearance = tabBarAppearance
        tabBar.scrollEdgeAppearance = tabBarAppearance
        
    }

Please make sure, that we use this code in TabBar class only, to get the desired results.

Related