I've created a CAGradientLayer
, which I'd like to add as a sublayer to a UIView
. I can add it to self.view.layer
with no problems, but cannot for the life of me get it to appear when added to the UIView
.
Here's the simplified code.
- (CAGradientLayer*) makeGradient
{
//method returns the gradient layer
CAGradientLayer *gradeLayer = [CAGradientLayer layer];
gradeLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
gradeLayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.2], [NSNumber numberWithFloat:0.5], nil];
return gradeLayer;
}
-(void)addGradient
{
//method creates UIView, then creates Gradient, and tries to add it to the UIView
UIView *myView = [[UIView alloc] initWithFrame:self.view.frame];
myView.backgroundColor = [UIColor clearColor];
myView.opaque = NO;
CAGradientLayer *bg = [self makeGradient];
CGRect myRect = myView.bounds;
myRect.size.height = myRect.size.height * 5;
myRect.origin.y = myView.bounds.size.height-myRect.size.height;
bg.frame = myRect;
//Adding gradient to self.view.layer works like a charm...
//[self.view.layer insertSublayer:bg atIndex:0];
//...however, adding it to my custom view doesn't work at all.
[myView.layer insertSublayer:bg atIndex:0];
}
What am I missing? Thanks in advance for any insight.