How can I set UITabBarAppearance() without overriding UITabBarItem.appearance()?

Viewed 304

I style my UITabBarItem titles globally as follows:

UITabBarItem.appearance().setTitleTextAttributes(TextStyle.tabTitle(colored: false).attributes, for: .normal)
UITabBarItem.appearance().setTitleTextAttributes(TextStyle.tabTitle(colored: true).attributes, for: .selected)

In iOS 15, I need to override the new behaviour where the tab bar background is hidden at the scroll edges. I can do that like this:

let appearance = UITabBarAppearance()
appearance.configureWithDefaultBackground()
tabBar.standardAppearance = appearance
tabBar.scrollEdgeAppearance = appearance

However, this seems to override the font of the tab bar item titles, and reset them back to the default. I can't seem to override the tab bar hiding behaviour, and retain my custom font.

1 Answers

You can use

    let tabBarItemAppearance = UITabBarItemAppearance()
    tabBarItemAppearance.normal.titleTextAttributes = [TextStyle.tabTitle(colored: false).attributes]
    tabBarItemAppearance.selected.titleTextAttributes = [TextStyle.tabTitle(colored: false).attributes]
Related