I have a function that accepts a UIColor.
func getColor(_ background: UIColor) -> UIColor {
switch background {
case .white, .systemBrown:
return .black
case .darkGray:
return .lightGray
case .black:
return .white
default:
return .label
}
}
and I have UIButton with background color .systemBrown
let brownButton: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.backgroundColor = .systemBrown
btn.layer.borderWidth = 1.0
btn.layer.borderColor = UIColor.brown.cgColor
return btn
}()
When i call the function like this:
getColor(brownButton.backgroundColor!)
It returns .label (default case). But when i use:
getColor(.systemBrown)
I get the expected result
2
Answers
It might be that the system is doing something to the background color like applying a tint to it.
I would suggest logging the RGB and alpha values of UIColor.systemBrown and of your brownButton’s backgroundColor. You might find that they are slightly different. In that case you might need to write a "color close to another color" comparison. (Say the R, G, and B components are within 0.02 of each other? Or within 5 for integer values from 0-255. I just pulled those ranges out of thin air, but they are probably a good starting point.)
Something like this:
The "system" colors are dynamic based on the trait collection of what they are applied to.
Printing the various colors gives you an idea of the differences:
The output is:
This indicates that applying the
systemBrown
color to the view changed the color’s properties a bit. This means it is no longer equal to a "plain"systemBrown
color any more.One solution is to resolve all relevant colors to the appropriate trait collection.
Update
getColors
by resolving any dynamic colors used incase
statements:Then update your call to
getColors
:You need to resolve the passed background color as well as pass the view’s
traitCollection
togetColors
.If you always work on a view’s
backgroundColor
you could changegetColors
to take just aUIView
parameter and resolve the colors inside that function as needed.