Getting Default Unselected TintColor of UITabBarItem Image

170 views Asked by At

how do we get the default color of unselected UITabBarItem Image?

Searched all over SO only returns methods to change, while what I want is only to get the color.

Any idea?

enter image description here

1

There are 1 answers

4
Henri Normak On

As far as I remember (I looked into something similar about a year ago) there is no system API to get the "dimmed" colour of inactive controls. I did however reverse engineer the dimming to look relatively similar.

public extension UIColor {
    public func dimmedColor() -> UIColor {
        var hue = CGFloat(0)
        var brightness = CGFloat(0)
        var saturation = CGFloat(0)
        var alpha = CGFloat(0)

        self.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)

        brightness = max(0.0, brightness - 0.3)

        return UIColor(hue: hue, saturation: 0, brightness: brightness, alpha: alpha)
    }
}

In essence, the dimming desaturates the colour and reduces brightness. This can result in black colours, which might not be what you want though.

You would use this as follows, where tintColor is obtained from a UIView or a button or some other tinted element:

let dimmedColor = tintColor.dimmedColor()