Valentin Shamardin
Valentin Shamardin

Reputation: 3658

iOS 15 UITabBarItem customization

I have UITabBarConroller subclass and custom items. Customization looks like this:

private func updateTabBar() {
    guard let items = tabBar.items,
        tabViewControllers.count == items.count else {
        return
    }

    for (item, vc) in zip(items, tabViewControllers) {
        // setting items title, image, selectedImage ...
        item.setTitleTextAttributes(Self.normalAttributes, for: .normal)
        item.badgeColor = .customColor
    }
}

This works fine on iOS 14 and earlier. But not on iOS 15. I have found some information about scrollEdgeAppearance. This prevents black background. But setting colored text attributes and badge color is ignored. The question is how to set custom text color and badge color on the UITabBarItem?

Upvotes: 3

Views: 2850

Answers (1)

Valentin Shamardin
Valentin Shamardin

Reputation: 3658

Finally, I did it. I was looking for stackedLayoutAppearance and its properties iconColor and badgeBackgroundColor. Depending on your project you may need also inlineLayoutAppearance or compactInlineLayoutAppearance.

@available(iOS 13.0, *)
private static let tabBarAppearance: UITabBarAppearance = {
    let appearance = UITabBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = .customBackgroundColor
            
    appearance.stackedLayoutAppearance.normal.iconColor = .customTitleColorForNormalState
    appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
    appearance.stackedLayoutAppearance.selected.titleTextAttributes = normalAttributes
    appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .customBadgeColor
    return appearance
}()

And I use this static constant in my loop through all UITabBarItems:

if #available(iOS 13.0, *) {
    item.standardAppearance = Self.tabBarAppearance
    if #available(iOS 15.0, *) {
        item.scrollEdgeAppearance = item.standardAppearance
    }
}

Upvotes: 4

Related Questions