Search code examples
iosswiftuicolor

Accessing the system .Destructive red button color


How do you access the system red color used for the destructive alert button style?

For instance, the default style blue color used for buttons can be accessed in Swift by let systemBlueColor = view.tintColor, which corresponds to UIColor(red: 0, green: 122, blue: 255, alpha: 1.0).

The destructive red color seems to be given by UIColor(red: 255, green: 59, blue: 48, alpha: 1.0) but is there any way to access it in a similar way to the default view.tintColor method?

I have read that RGB interpretations can vary on devices / operating systems, so I would like to access the device / operating system independent version of the color.


Solution

  • There is an undocumented class method on UIColor called _systemDestructiveTintColor which will return the color you need:

    let red = UIColor.performSelector("_systemDestructiveTintColor").takeUnretainedValue()
    

    It returns an unmanaged object, which you must call .takeUnretainedValue() on, since the color ownership has not been transferred to our own object.

    As with any undocumented API, you should take caution when trying to use this method:

    Swift 5:

    if UIColor.responds(to: Selector(("_systemDestructiveTintColor"))) {
        if let red = UIColor.perform(Selector(("_systemDestructiveTintColor")))?.takeUnretainedValue() as? UIColor {
            // use the color
        }
    }
    

    Previous Swift versions:

    if UIColor.respondsToSelector("_systemDestructiveTintColor") {
        if let red = UIColor.performSelector("_systemDestructiveTintColor").takeUnretainedValue() as? UIColor {
            // use the color
        }
    }
    

    This and other colors can be found in the UIColor header.