0

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

JayVDiyk
  • 4,277
  • 22
  • 70
  • 135

1 Answers1

0

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()
Henri Normak
  • 4,695
  • 2
  • 23
  • 33
  • Interesting answer indeed, but it creates a lighter version than the actual unselected color – JayVDiyk Jun 08 '15 at 02:48
  • Hmm, I suppose the brightness value I ended up at is not exactly what they use, you could tweak it a bit to see if you get any closer. – Henri Normak Jun 08 '15 at 06:31
  • I've tried using different tint colors, that does not affect the unselected color though. it's #929292. I need a way to determine the system color, to anticipate if Apple ever changes the default color – JayVDiyk Jun 08 '15 at 06:32
  • As I said, from what I know there is now way to do so. You could try to capture the colour, for example by creating a control, letting the system dim it, capturing it in an image and then looking at the colour values. – Henri Normak Jun 08 '15 at 08:16