Search code examples
ioscalayerquartz-core

Cannot Call addSubLayer:


I am trying to add a gradient to my view and am adding the following to drawRect:

CAGradientLayer *g = [[CAGradientLayer alloc]init];
g.frame = self.bounds;
g.colors = [NSArray arrayWithObjects:(id)[[UIColor blueColor] CGColor],
            [[UIColor redColor] CGColor], nil];
g.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
               [NSNumber numberWithFloat:1.0],
                nil];
[self addSubLayer:g];

I receive the warning: No visible @interface declares the selector 'addSubLayer:'

although I imported the Quartz Core framework since CALayer is part of it (otherwise I would have already received an error when I initialized the CAGradientLayer). Where am I going wrong?


Solution

  • I believe that line should be (note capitalization and the use of self.layer):

    [self.layer addSublayer:g];
    

    addSublayer is a CALayer instance method, not a UIView instance method.