I am trying to add a gradient to a CALayer I have created. I can set the background colour of the CALayer with the following:
self.colorLayer = [CALayer layer];
[self.colorLayer setBackgroundColor:color.CGColor];
[self.colorView setWantsLayer:YES];
[self.colorView setLayer:self.colorLayer];
I've looked around with no success (surprisingly I thought this would have been answered many times).
I have made a gradient with:
NSGradient *gradient =
[[NSGradient alloc] initWithStartingColor:[NSColor orangeColor]
endingColor:[NSColor lightGrayColor]];
But can't add it to my CALayer or add an angle.
Swift 4 version of this answer
You don't need to add a gradient to a CALayer, because you can use a CAGradientLayer instead.