UI Tab bar is not changing if UITabBarAppearance is active?

146 views Asked by At

I'm trying to change the color of the unselected tab item in my UITabBar using the unselectedItemTintColor property. However, the color is not changing when I set it in code. Here's the relevant code:

init() {
let appearance = UITabBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = UIColor.systemGray6

let image = UIImage.gradientImageWithBounds(
    bounds: CGRect(x: 0, y: 0, width: UIScreen.main.scale, height: 20),
    colors: [
        UIColor.clear.cgColor,
        UIColor.black.withAlphaComponent(0.12).cgColor
    ]
)
appearance.backgroundImage = UIImage()
appearance.shadowImage = image

UITabBar.appearance().standardAppearance = appearance
UITabBar.appearance().unselectedItemTintColor = UIColor(Color("indigo")) // problem here
}
extension UIImage {
    static func gradientImageWithBounds(bounds: CGRect, colors: [CGColor]) -> UIImage {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = colors
        
        UIGraphicsBeginImageContext(gradientLayer.bounds.size)
        gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

As you can see, I'm setting the UITabBar appearance to a custom UITabBarAppearance object, and then setting the unselectedItemTintColor property to a custom color defined in my asset catalog ("indigo").

However, the color of the unselected tab item is not changing, even though all other appearance properties are working correctly (e.g., the background color and shadow image).

I've tried commenting out all other appearance properties except for unselectedItemTintColor, and it's helping!.

Is there something I'm missing or doing wrong? Any help would be appreciated. Thank you!

enter image description hereenter image description here

0

There are 0 answers