Search code examples
iosswiftuicolor

How to correctly get and set UIColor for UIView?


I'm trying to change a UIView's backgroundColor based on its current color. E.g. say MyView is now red, on touch I want to make it blue and vice versa. But I noticed I cannot get the correct name of the UIColor to check the current color.

As an example, if I do:

if MyView.backgroundColor == .systemRed {
        print("Red")
    } else {
        print("Not red")
    }

This will always print "Not red", even if I explicitly set MyView.backgroundColor = .systemRed

When I check the color being used:

print("Color = \(MyView.backgroundColor)")

It prints: [...] name = systemRedColor

But when I change .systemRed to .systemRedColor, it gives the error

'systemRedColor' has been renamed to 'systemRed'

and I'm back at where I started.

Can anyone tell me what I am missing? How do I correctly read and set the backgroundColor of UIView?


Solution

  • .systemRed is one of the dynamic system colours that will be displayed differently depending on the view's current trait condition (i.e. Dark Mode / Light Mode). Therefore you need to access the current color using the .resolvedColor(with traitCollection:) function.

    In Light Mode, systemRed will display as: UIExtendedSRGBColorSpace 1 0.231373 0.188235 1 R255 G59 B48

    In Dark Mode, systemRed will display as: UIExtendedSRGBColorSpace 1 0.270588 0.227451 1 R255 G69 B58

    if view.backgroundColor?.resolvedColor(with: view.traitCollection) == UIColor.systemRed.resolvedColor(with: view.traitCollection) {
        print("true")
    }
    

    This will work with both dynamic and standard UIColor's.


    You could also make an extension on UIColor to do this

    extension UIColor {
        class func compare(_ colorA: UIColor?, with colorB: UIColor?, in view: UIView) -> Bool {
            colorA?.resolvedColor(with: view.traitCollection) == colorB?.resolvedColor(with: view.traitCollection)
        }
    }
    

    Usage:

    if UIColor.compare(self.view.backgroundColor, with: .systemRed, in: view) {
        print("true")
    }