Wrong color on CAGradient applied to UITextView - uitableview

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.

Related

Adding CAGradient layer but unable to get Gradient effect

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

CAGradientLayer is going out of frame of UILabel

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;

CAGradientLayer for only background

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.

ios push view with gradient background shows overlap

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.

CAGradientLayer showing as solid color

I'm trying to set a gradient background on a view, but my code below makes the UIView appear solid black. If i change the whiteColor to greenColor, the gradient is displayed properly. Changing the backgroundColor of the layer to greenColor makes the view appear solid green.
My guess is that i'm dealing with some kind of transparency-issue, but i can't figure to work around it.
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = [NSArray arrayWithObjects:
(id)[[UIColor whiteColor] CGColor],
(id)[[UIColor blackColor] CGColor], nil];
gradientLayer.frame = CGRectMake(0, 0, 1, someView.frame.size.height);
UIGraphicsBeginImageContext(CGSizeMake(1, someView.frame.size.height));
[gradientLayer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
someView.backgroundColor = [UIColor colorWithPatternImage: image];
Your white and black colors are in grayscale colorspace. Try making them in RGB colorspace:
gradientLayer.colors = #[
(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor,
(id)[UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor
];
This should fix your issue, though I'm not sure of the Quartz mechanics behinds this.

Resources