I am extending UITableView and overriding setBackgroundView to add a gradient to cell in a grouped style table view. All good so far...
In order to apply rounded corners to the cell, I set the cornerRadius to 10 which looks about right.
All is working well but I don't like the idea of hardcoding the cornerRadius. Is this constant somewhere in the Cocoa code base?
Example:
- (void) setBackgroundView:(UIView *)backgroundView{
NSLog(#"called setBackground");
NSLog(#"%f", backgroundView.layer.shadowRadius); //yields 0.00000
NSLog(#"%f", backgroundView.layer.cornerRadius); //yields 3.00000
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setCornerRadius:10];
[gradient setMasksToBounds:YES];
[gradient setBorderWidth:0.8f];
[gradient setBorderColor:[[UIColor darkGrayColor] CGColor]];
gradient.frame = backgroundView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor greenColor] CGColor], (id) [[UIColor colorWithRed:.05 green:.65 blue:.05 alpha:1] CGColor],(id)[[UIColor colorWithRed:.05 green:.65 blue:.05 alpha:1] CGColor], (id)[[UIColor colorWithRed:.05 green:.45 blue:.05 alpha:1] CGColor], nil];
gradient.locations = [NSArray arrayWithObjects: (id)[NSNumber numberWithFloat:0.10], (id)[NSNumber numberWithFloat:0.50], (id)[NSNumber numberWithFloat:0.50], (id)[NSNumber numberWithFloat:1.0], nil];
[backgroundView.layer insertSublayer:gradient atIndex:0];
[super setBackgroundView:backgroundView]; }
Related
I have used below code to set gradient background color of my view.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = #[(id)[UIColor colorWithRed:44/255 green:58/255 blue:76/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17/255 green:22/255 blue:30/255 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
But its not working. Screen will appear with black color. But when I try to set color like below
gradient.colors = #[(id)[UIColor blueColor].CGColor, (id)[UIColor redColor].CGColor];
then it working fine.I am getting correct output. Can anyone help me to fine why gradient color is not working with UIColor colorWithRed function.
Thanks in advance.
For the original poster's code to work, he just has to divide each color component by 255.0 instead of 255, if not the result is truncated to an int.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = #[(id)[UIColor colorWithRed:44/255.0 green:58/255.0 blue:76/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17/255.0 green:22/255.0 blue:30/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
Alternatively, make the dividend a float value:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = #[(id)[UIColor colorWithRed:44.0/255 green:58.0/255 blue:76.0/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255 green:22.0/255 blue:30.0/255 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
Or the most easy to follow solution, make both numbers in the division a float value:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = #[(id)[UIColor colorWithRed:44.0/255.0 green:58.0/255.0 blue:76.0/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255.0 green:22.0/255.0 blue:30.0/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
Try this:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:202.0/255.0 green:107.0/255.0 blue:122.0/255.0 alpha:1] CGColor], (id)[[UIColor colorWithRed:192.0/255.0 green:144.0/255.0 blue:120.0/255.0 alpha:1] CGColor], nil];
gradient.frame = self.view.bounds;
[self.view.layer insertSublayer:gradient atIndex:0];
Try like this!
-(CAGradientLayer *)makeGradientView :(UIColor *)topColor BottomColor:(UIColor *)bottomColor withFrame:(CGRect )rect
{
CAGradientLayer *theViewGradient = [CAGradientLayer layer];
NSArray *gradientLocations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0], nil];
theViewGradient.locations = gradientLocations;
theViewGradient.colors = [NSArray arrayWithObjects: (id)topColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor, (id)bottomColor.CGColor, nil];
theViewGradient.frame = rect;
theViewGradient.startPoint = CGPointMake(1,1);
theViewGradient.endPoint = CGPointMake(1.0,0.1);
return theViewGradient;
}
And you can use like this!
#property (weak, nonatomic) IBOutlet UIView *mainView;
CAGradientLayer *theViewGradient = [self makeGradientView:[UIColor colorWithRed:189/255.0 green:227/255.0 blue:106/255.0 alpha:0.5] BottomColor:[UIColor colorWithRed:13/255.0 green:184/255.0 blue:196/255.0 alpha:0.5] withFrame:self.view.bounds];
[_mainView.layer addSublayer:theViewGradient];
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
Within my 'cellForRowAtIndexPath' I have the following code to populate the image (async) and apply a custom gradient
This worked fine, until I added a custom colour per cell. What it's currently doing is recycling the previous colour rather than applying a new one - this is presumably due to the following line which will skip the gradient code once applied to each cell:
if(!cell.gradientMask){
However, if I comment this out, the colours work but the gradient on each cell will stack up as a new layer is added each time (see existing issue)
I presume I need to remove the gradientLayer on each iteration, is that the best approach or perhaps I need to subclass the UIImageView?
if (!cell.gradientMask) {
gradientMask = [CAGradientLayer layer];
gradientMask.frame = cell.eventImage.layer.bounds;
gradientMask.startPoint = CGPointMake(0.5, 0.2);
gradientMask.endPoint = CGPointMake(0.5, 1.0);
/* THIS COLOUR CHANGES FOR EACH CELL */
gradientMask.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:0.0f] CGColor],
(id)[[UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f] CGColor],nil];
[cell.eventImage.layer insertSublayer:gradientMask atIndex:0];
cell.gradientMask = gradientMask;
}
You need to set the colors outside the block which creates the gradientMask:
if (!cell.gradientMask) { //Operations that need to be carried out only ONCE are put inside this block
gradientMask = [CAGradientLayer layer];
gradientMask.frame = cell.eventImage.layer.bounds;
gradientMask.startPoint = CGPointMake(0.5, 0.2);
gradientMask.endPoint = CGPointMake(0.5, 1.0);
[cell.eventImage.layer insertSublayer:gradientMask atIndex:0];
cell.gradientMask = gradientMask;
}
//Operations that need to be carried out again and again are outside the block
if (condition1)
{
cell.gradientMask.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:0.0f] CGColor],
(id)[[UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f] CGColor],nil];
}
else
{
cell.gradientMask.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:0.0f green:1.0f blue:0.0f alpha:0.0f] CGColor],
(id)[[UIColor colorWithRed:0.0f green:1.0f blue:0.0f alpha:1.0f] CGColor],nil];
}
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
Before iOS 8, able to set a gradient background to a UIPickerView so the spinner would show on top and behind it, a gradient background as shown here:
Here is the code for the above prior to iOS 8:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = pickerView.bounds;
gradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor blackColor] CGColor],
(id)[[UIColor darkGrayColor] CGColor],
(id)[[UIColor lightGrayColor] CGColor],
(id)[[UIColor whiteColor] CGColor],
(id)[[UIColor lightGrayColor] CGColor],
(id)[[UIColor darkGrayColor] CGColor],
(id)[[UIColor blackColor] CGColor],
nil];
[pickerView.layer insertSublayer:gradient atIndex:0];
Using the above code in iOS 8, here is what displays:
Both pickers still function, but as you can see, the UI is not acceptable.
Suggestions? Thanks in advance!
Okay, I found a work-around, but still open to other, better options. My solution is:
Make UIPickerView backgroundColor transparent
Place a UILabel behind the UIPickerView
Create my gradient as above and add it to the UILabel as a subLayer :atIndex 0
Here is the code sample:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = colorPickerBackgroundLabel.bounds;
gradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor blackColor] CGColor],
(id)[[UIColor darkGrayColor] CGColor],
(id)[[UIColor lightGrayColor] CGColor],
(id)[[UIColor whiteColor] CGColor],
(id)[[UIColor lightGrayColor] CGColor],
(id)[[UIColor darkGrayColor] CGColor],
(id)[[UIColor blackColor] CGColor],
nil];
[colorPickerBackgroundLabel.layer addSublayer:gradient];
Here is the finished screen area capture: