iOS白色到透明渐变层是灰色的

我有一个CAGradientLayer插入到这个小的细节视图的底部,popup在应用程序的底部。 正如你所看到的,我已经把白色的颜色设置为清晰,但是这个奇怪的灰色正在显现。 有任何想法吗?

// Set up detail view frame and gradient [self.detailView setFrame:CGRectMake(0, 568, 320, 55)]; CAGradientLayer *layer = [CAGradientLayer layer]; layer.frame = self.detailView.bounds; layer.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor, nil]; layer.startPoint = CGPointMake(1.0f, 0.7f); layer.endPoint = CGPointMake(1.0f, 0.0f); [self.detailView.layer insertSublayer:layer atIndex:0]; 

这是有问题的观点:

这是有问题的观点

clearColor有一个alpha为0的黑色通道,所以我不得不使用

 [UIColor colorWithWhite:1 alpha:0] 

它工作正常。

斯威夫特这对我工作,

 UIColor.white.withAlphaComponent(0).cgColor 

Swift 3语法,

 UIColor(white: 1, alpha: 0).cgColor 

值得指出的是,任何其他颜色将像这样工作…使用上述两个答案的组合….

目标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 = .red let colours:[CGColor] = [colour.withAlphaComponent(0.0).cgColor,colour.cgColor] let locations:[NSNumber] = [0.2,0.8] let gradientLayer = CAGradientLayer() gradientLayer.colors = colours gradientLayer.locations = location gradientLayer.frame = frame layer.addSublayer(gradientLayer)