I am setting gradient to background of my view:
CAGradientLayer *grad = [CAGradientLayer layer];
grad.frame = self.contentView.frame;
grad.colors = #[(id)[[UIColor colorWithRed:1.0f green:0.5f blue:0.5f alpha:0.0f] CGColor],
(id)[[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor],
(id)[[UIColor colorWithWhite:1.0f alpha:0.8f] CGColor],
(id)[[UIColor colorWithWhite:0.0f alpha:1.0f] CGColor]];
grad.locations = #[#0.00f, #0.5f, #0.7f, #1.00f];
self.layer.mask = grad;
Problem is, that every element (UIButton, UILabel...) on top of my view has that then same gradietn as parent. How can I set gradient only for view and not for items, that are on this view ?
Instead of setting the mask, you can set the gradient as sublayer of your view as below:
[self.view setBackgroundColor:[UIColor clearColor]];
[self.view.layer insertSublayer:grad atIndex:0];
That is what a mask is supposed to do. :-)
You can work with a dedicated background view that is not superview (but probably sibling) to the other views that are drawn on top of it.
When you don't use it as superview of subviews but as sibling to siblings (all share the same superview) then the sequence is of importance. Make sure that the background view is drawn first. That is in IB when it is located above of the "subviews" in the view hierarchy tree but within the same level.
Related
Here i am using this code to give a gradient effect but not getting the effect at all.
CAGradientLayer *layer2 = [CAGradientLayer layer];
NSArray *gradientColors = [NSArray arrayWithObjects:(id)[self colorWithHexString:#"3eb79d"].CGColor,(id)[self colorWithHexString:#"3fb65c"].CGColor,(id)[self colorWithHexString:#"3cb7da"].CGColor, nil];
[layer2 setColors:gradientColors];
[layer2 setFrame:cell.userBackgroundView.layer.frame];
[cell.userBackgroundView.layer insertSublayer:layer2 atIndex:0];
cell.userBackgroundView.clipsToBounds = YES;
Try this
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame =cell.userBackgroundView.frame;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[cell.userBackgroundView.layer insertSublayer:gradient atIndex:0];
Try to change index of sublayer from 0 to -1
[cell.userBackgroundView.layer insertSublayer:layer2 atIndex:-1];
Make sure your userBackgroundView is not covered by another view.
Use
[layer2 setFrame:cell.userBackgroundView.bounds];
instead of
[layer2 setFrame:cell.userBackgroundView.layer.frame];
If you are using autoLayout, make sure to set the frame in viewDidAppear or viewDidLayOutSubviews
I am doing simple things. As I am learning to implement CAGradientLayer.
The gradient you see is of Label. I am using autoresizing(not Autolayout). Apart from this have no code written. Autoresizing is not a problem since I've not touched that.
CAGradientLayer *gradientLayer = [[CAGradientLayer alloc]init];
gradientLayer.frame = lblHolder.frame;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor greenColor] CGColor], nil];
[lblHolder.layer insertSublayer:gradientLayer atIndex:0];
Try to make the following change:
gradientLayer.frame = lblHolder.bounds;
I have an issue with the gradient color, I'm not able to make the gradient color appear as circular, it always appear like linear, i need the exact gradient in the following image:
PS: this is the code i use:
UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.startPoint = CGPointMake(0.0,0.4);
gradient.endPoint = CGPointMake(0.0,0.6);
view.layer.masksToBounds = YES;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:153.0/255.0 green:204.0/255.0 blue:0.0/255.0 alpha:1.0] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:view];
There is a CGContextDrawRadialGradient in CGContext. I hope it will help you
I use a gradient background for my views. When I push a new view using a segue, old view slides left and new view slides in - BUT for a brief moment the old view is still visible in the background and then abruptly turns off before the new view is in completely. Overall this is not smooth.
I am using this piece of code to set the gradient background...
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = vc.view.bounds;
UIColor *startColor = [UIColor colorWithRed:0.5 green:0.75 blue:0.75 alpha:0.98];
UIColor *endColor = [UIColor colorWithRed:0.5 green:0.75 blue:0.75 alpha:0.10];
gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
[vc.view.layer insertSublayer:gradient atIndex:0];
vc.navigationController.navigationBar.barTintColor = startColor;
Any idea what I am doing wrong ? Sorry if I haven't furnished all the information.
I'm creating an iOS7 app and I have an UITextView inside a UITableView cell with a CAGradient applied to it. If I use different colors from clearColor works fine, but if I use clearColor as one of the colors for the gradient it turns out to be gray...
This is my code. theContent is my UITextView.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = theContent.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[theContent.layer insertSublayer:gradient atIndex:100];
And this is the final (wrong) result...
How can I fix this to have a clearColor to whiteColor gradient???
it may help on you if you try to use
[UIColor colorWithWhite:1.f alpha:0.f];
instead of the clearColor for the white tone.