I’m having some problems applying my gradient to more than one view, here’s the code:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view6.bounds;
gradient.frame = view7.bounds;
gradient.frame = view8.bounds;
gradient.frame = view9.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1.0] CGColor], nil];
[view6.layer insertSublayer:gradient atIndex:0];
[view7.layer insertSublayer:gradient atIndex:0];
[view8.layer insertSublayer:gradient atIndex:0];
[view9.layer insertSublayer:gradient atIndex:0];
So what am I doing wrong? At the moment, the gradient is only being applied to view9 which has left me a bit stumped. Any suggestions are welcomed.
Thanks!
You should create layer every time when you want to add it to sublayer.
Good approach is create
GradientView:UIViewclass than overridedrawRectmethod and draw gradient or add gradient layer to subview at another place in code. This way more useful, because if you want to change color of gradient you must change the code only in one place(GradientView).Another way is create method that will be build the gradient layer for each view.
…