Search code examples
cocoacore-animationcalayer

Calling -[CALayer setBorderColor:] with an RGB color


Hell again all. I seem to be unable to assign RGBA colors to the setBorderColor method of a layer.

I tried:

UIColor *myColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
[l setBorderColor:myColor];

Where l is of type CALayer and I get the warning: Incompatible pointer types sending 'UIColor*' to parameter of type 'CGColorRed ('aka 'struct CGColor *'). Do you know what the reason may be? The warning appears on the last line. On the internet I find this code over and over again, so I thought it should be valid... Thanks!


Solution

  • CALayer.borderColor is defined as

    @property CGColorRef borderColor;
    

    Note that the type here is CGColorRef. You're trying to pass a UIColor*, which is a different beast. Luckily, UIColor has a property that returns a CGColorRef. Try using

    [l setBorderColor:myColor.CGColor];