2

This doesn't work:

if([myView.backgroundColor isEqual:[UIColor clearColor]])
   NSLog("Background is clear");
else
   NSLog("Background is not clear");

P.S: To reproduce the case, drag a uiview in interface builder, set it's background color to clear color from interface builder. Set an outlet of the view, then compare it in viewDidLoad using the code above.

Here is the link of the test project: https://drive.google.com/file/d/0B_1hGRxJtrLjMzUyRHZyeV9SYzQ/view?usp=sharing

Here is the Snapshot of Interface Builder: enter image description here

Chanchal Raj
  • 4,176
  • 4
  • 39
  • 46
  • 2
    There is no reason why that shouldn't work. what makes you think it doesn't work? – villy393 Jul 22 '15 at 14:17
  • @villy393: The NSLog result "Background is not clear" makes me think it doesn't work because, I have set background color of the view to clear color from interface builder. – Chanchal Raj Jul 22 '15 at 14:23
  • I think there might be something else going on. If you compare `[[UIColor clearColor] isEqual:[UIColor clearColor]]` it will return true. – villy393 Jul 22 '15 at 14:26
  • It will obviously return true. Ok to reproduce the case, drag a uiview in interface builder, set it's background color to clear color from interface builder. Set an outlet of the view, then compare it in viewDidLoad. – Chanchal Raj Jul 22 '15 at 14:49
  • @ChanchalRaj check my updated answer, it should solve the issue – Alaeddine Jul 22 '15 at 15:13

3 Answers3

5

Maybe both colors are clear but still not the same? It's not enough that the alpha value is the same...

UIColor.clearColor() == UIColor(white: 1.0, alpha: 0.0) // false
UIColor.clearColor() == UIColor(white: 0.0, alpha: 0.0) // true

Try

if([myView.backgroundColor isEqual:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]])
   NSLog("Background is clear");
else
   NSLog("Background is not clear");

Even better is probably to just check if the alpha is 0.0:

CGFloat alpha = CGColorGetAlpha(myView.backgroundColor.CGColor);
if( alpha == 0.0 )
   NSLog("Background is clear");
else
   NSLog("Background is not clear");
Stefan
  • 1,496
  • 1
  • 13
  • 26
  • What I have done is dragged a UIView (was of white color by default), set its background color to clear from interface builder. Put an outlet to the uiview and then wrote the above code in viewDidLoad. What do you think is wrong, pal? – Chanchal Raj Jul 22 '15 at 14:52
  • It appears interface builder creates a black RGB image with alpha 0 and clearColor is a black grayscale image with alpha 0. Looks the same, but doesn't compare the same. if( [self. myView.backgroundColor isEqual:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0] ] – Stefan Jul 22 '15 at 15:13
4

You code is correct

UIView *aView = [[UIView alloc] init];
aView.backgroundColor = [UIColor clearColor];

if([aView.backgroundColor isEqual:[UIColor clearColor]]) {
     NSLog(@"Background is clear");
} else {
     NSLog(@"Background is not clear");
}

Result:

2015-07-22 15:22:57.430 APP[1568:24173] Background is clear

UPDATE: case of default color from Interface builder

From : UIView Class Reference

@property(nonatomic, copy) UIColor *backgroundColor

Discussion

Changes to this property can be animated. The default value is nil, which results in a transparent background color.

if you set color to be default from interface builder backgroundColor property will be nil so the comparison to [UIColor clearColor] will be false.

you can update you code the handle this case:

if([self.aView.backgroundColor isEqual:[UIColor clearColor]] ||  self.aView.backgroundColor == nil) {
    NSLog(@"Background is clear");
} else {
    NSLog(@"Background is not clear");
}

Default color

UPDATE: case of clear color from Interface builder

clear color

You can test the alpha value :

CGFloat bgColorAlpha = CGColorGetAlpha(self.aView.backgroundColor.CGColor);

if (bgColorAlpha == 0.0){
    NSLog(@"clear ");
}else{
   NSLog(@"not clear ");
}

2015-07-22 16:56:23.290 APP[1922:38283] clear

More information:

How to compare UIColors

Comparing colors in Objective-C

UIColor comparison

Community
  • 1
  • 1
Alaeddine
  • 6,104
  • 3
  • 28
  • 45
  • I didn't initialised the view in code, but from interface builder. To reproduce the case, drag a uiview in interface builder, set it's background color to clear color from interface builder. Set an outlet of the view, then compare it in viewDidLoad. – Chanchal Raj Jul 22 '15 at 14:54
  • @ChanchalRaj check my update I have tested the code with a created `UIView` from IB – Alaeddine Jul 22 '15 at 15:10
  • Nope, pal, it didn't work too. backgroundColor property doesn't turn nil if we choose clearColor from interface builder. Here is an NSLog for the statement: NSLog("view color: %@", myView.backgroundColor); view color: UIDeviceRGBColorSpace 0 0 0 0 – Chanchal Raj Jul 22 '15 at 15:36
  • And if I NSLog this statement: NSLog("expected color: %@", [UIColor clearColor]); It prints: expected color: UIDeviceWhiteColorSpace 0 0 – Chanchal Raj Jul 22 '15 at 15:38
  • I have dragged a new `UIView` and setup the background color to clear and NSLog gives me nil, did you change any other property from interface builder, alpha for example ? – Alaeddine Jul 22 '15 at 15:40
  • Your background color show "Default" not "Clear Color". "Default" is nil and "Clear Color" is a black RGB color with alpha 0. – Stefan Jul 22 '15 at 15:47
  • Nope, the selected background color is 'Clear Color', not 'Default'. Here is the link of the project: https://drive.google.com/file/d/0B_1hGRxJtrLjMzUyRHZyeV9SYzQ/view?usp=sharing – Chanchal Raj Jul 22 '15 at 15:56
  • @ChanchalRaj I have updated the answer to treat the the case of an `UIView` created by code, `UIView` create in IB with default color and `UIView` created by IB with clear color – Alaeddine Jul 22 '15 at 16:00
  • @Aladin: Thanks a lot. I needed a method to get alpha value of some color, and you nailed it out! Great. – Chanchal Raj Jul 22 '15 at 16:06
  • alpha is a CGFloat not a NSInteger. You may get unexpected results from that comparison. – Stefan Jul 22 '15 at 16:08
  • @Stefan there is a cast `(NSInteger)` before comparing – Alaeddine Jul 22 '15 at 16:15
  • Sure, but it truncates the value so bgColorAlpha will contain 1 if alpha = 1.0 and 0 in all other cases. 0.9999 if far from clear, but will be treated as clear, and probably not what @Chanchal Raj expected. – Stefan Jul 22 '15 at 16:43
  • @Stefan sure, but in the case of clear color alpha is 0.0000 so there won't be any problem, but I am open to any suggestion to improve the answer if you have one – Alaeddine Jul 22 '15 at 17:35
  • 1
    It could be a problem since your code says it's clear even when the alpha is 0.9999 which is practically opaque. My suggestion is like in my answer. Keep the alpha as CGFloat and compare to 0.0 instead. Or possibly a small number like " if alpha < 0.01" – Stefan Jul 22 '15 at 17:40
  • @Stefan, Thanks pal. Your answer is perfect. Now let me know whose should I mark the correct one? :-) Both of you have done great toil to help me. – Chanchal Raj Jul 23 '15 at 10:56
  • Keep the answer from @Aladin as the correct one since it's very elaborate, but I would like to see him fix that integer truncation. :) – Stefan Jul 23 '15 at 11:27
  • @Aladin, the answer will more complete, I guess, if you include the last case of selecting some color (not default, not clear color) from interface builder too. – Chanchal Raj Jul 23 '15 at 13:29
  • @Aladin, when selecting some particular color the behavior is changed! Neither the backgroundColor becomes nil, nor if compared the backgroundColor property to the same color which was choosen from interface builder returns YES. – Chanchal Raj Jul 23 '15 at 13:45
0

Your code is correct, you are just having an incorrect assumption. Run these lines to see where you're going wrong:

NSLog("view color: %@", myView.backgroundColor);
NSLog("expected color: %@", [UIColor clearColor]);
Danny Bravo
  • 4,534
  • 1
  • 25
  • 43