I'd like to check the color set for a background on a UIImageView. I've tried:
if(myimage.backgroundColor == [UIColor greenColor]){
...}
else{
...}
but that doesn't work, even when I know the color is green, it always falls into the else part.
Also, is there a way to output the current color in the debug console.
p [myimage backgroundColor]
and
po [myimage backgroundColor]
don't work.
Some weird rounding errors can occur. That can be the reason a object set to a color and the color you set it to do not match exactly.
This is how I solved it:
This code can be improved by not only comparing 1 but comparing all the components. Eg red+green+blue+alpha == red2+green2+blue2+alpha2
This UIColor extension works fine provided that the compared colors can be converted into RGB format, which should be most of the cases.
At least with this extension:
Both comparisons would return false if compared using the native UColor.isEqual(...)
I wrote this category. If
isEqual:
does return NO, it will test if further comparison of different components might still match. If possible, different models are still compared.samvermette's solution translated to swift: