iOS White to Transparent Gradient Layer is Gray
clearColor has a black color channel with an alpha of 0, so I had to use
[UIColor colorWithWhite:1 alpha:0]
and it works fine.
In Swift This worked for me,
UIColor.white.withAlphaComponent(0).cgColor
Worth pointing out that any other colour will work like this... using a combination of the two answers above....
Objective C
UIColor *colour = [UIColor redColor];NSArray *colourArray = @[(id)[colour colorWithAlphaComponent:0.0f].CGColor,(id)colour.CGColor]NSArray *locations = @[@0.2,@0.8];CAGradientLayer *gradientLayer = [CAGradientLayer layer];gradientLayer.colors = colourArray;gradientLayer.locations = locations;gradientLayer.frame = self.frame;[self.layer addSublayer:gradientLayer];
Swift 3
let colour:UIColor = .redlet colours:[CGColor] = [colour.withAlphaComponent(0.0).cgColor,colour.cgColor]let locations:[NSNumber] = [0.2,0.8]let gradientLayer = CAGradientLayer()gradientLayer.colors = coloursgradientLayer.locations = locationsgradientLayer.frame = framelayer.addSublayer(gradientLayer)