How to correctly get and set UIColor for UIView?

寵の児 提交于 2021-01-27 13:18:14

问题


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?


回答1:


.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")
}



回答2:


You can try this.

self.bgView.backgroundColor = UIColor.red
 print("self.bgView.backgroundColor")

   if self.bgView.backgroundColor!.isEqual(UIColor.red){
      print("Red")
   } else {
   print("No Red")
 }
 print(self.bgView.backgroundColor)

//it will print Red



来源:https://stackoverflow.com/questions/61760182/how-to-correctly-get-and-set-uicolor-for-uiview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!