How to create a gradient

I have this code to create a gradient background

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
UIColor *startColour = [UIColor colorWithHexString:@"#bcdef6"];
UIColor *endColour = [UIColor colorWithHexString:@"#fad7e6"];
UIColor *middleColor = [UIColor colorWithHexString:@"#ffffff"];

gradient.colors = [NSArray arrayWithObjects:(id)[startColour CGColor],(id)middleColor, (id)[endColour CGColor], nil];
gradient.endPoint = CGPointMake(1, 0);
gradient.startPoint = CGPointMake(0, 1);
[self.layer insertSublayer:gradient atIndex:0];


how to get rid of the grayish effect in the middle?

this is the desired output: enter image description here

this is the actual result: enter image description here


source to share

1 answer

I see an error with your array of colors, you didn't take the middle color (white) CGColor. By changing the array to this, I can get the desired result.

gradient.colors = @[(id)startColour.CGColor, (id)middleColor.CGColor, (id)endColour.CGColor];


By playing around the gradient locations, you can also see more blues and pinks to create a better effect.

gradient.locations = @[@0.2f, @0.5f, @0.8f];




All Articles