How to change tab bar item text color programmatically in iOS 15? It doesn't work like in iOS 14.

If you try to change the bar item text color programmatically like in iOS 14, it doesn't work in iOS 15.

For example:

 // https://stackoverflow.com/questions/2576592/changing-font-size-of-tabbaritem/

 UITabBarItem.appearance().setTitleTextAttributes(
   [NSAttributedString.Key.foregroundColor: UIColor.red],
   for: .normal)

This correctly changes the color when the tab bar is on top of a scroll view.

However, when the tab bar background becomes clear, all tab bar item text is red and not just the text for the currently selected tab.

Is this a bug in iOS 15? Or is this code wrong for iOS 15?

Replies

It sounds like a bug if all of your items turn red when you scroll away, they should retain their individual colors. Please file a bug with an attached sample.

  • I am having the same issue. After settings scrollEdgeAppearance to standardAppearance to get an opaque tabBar, UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.red], for: .selected) doesn't do anything. titleTextAttributes returns nil and the text color defaults to systemBlue.

Add a Comment

Let's try to use stackedLayoutAppearance.

let tabBarAppearance = UITabBarAppearance()
let tabBarItemAppearance = UITabBarItemAppearance()

tabBarItemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
tabBarItemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]

tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance

tabBar.standardAppearance = tabBarAppearance
tabBar.scrollEdgeAppearance = tabBarAppearance