UIView上的绘图渐变不适用于iOS 9 [英] Drawing gradient on UIView not working with iOS 9
本文介绍了UIView上的绘图渐变不适用于iOS 9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用iOS 9在View Controller的背景视图上绘制渐变但是,我不知道为什么,不能正常工作
I'm trying to draw a gradient on the background view of a View Controller with iOS 9 but, I don't know why, isn't working
这是我的方法,从viewDidLoad调用
Here is my method, which is called from viewDidLoad
- (void)drawGradient
{
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = @[[UIColor greenColor], [UIColor redColor]];
gradient.locations = @[@0.0, @1.0];
[self.view.layer insertSublayer:gradient atIndex:0];
}
但没有任何反应,也没有出现渐变。
我做错了什么?
But nothing happens and gradient doesn't appear. What i'm doing wrong?
推荐答案
感谢Larme的评论,我发现了我的错误。
Thanks to Larme's comment, I figured out my mistake.
而不是
gradient.colors = @[[UIColor greenColor], [UIColor redColor]];
这是正确的
gradient.colors = @[(id)[UIColor greenColor].CGColor, (id)[UIColor redColor].CGColor];
因为 gradient.colors
需要 NSArray
CGColorRef
。为了创建 NSArray
,还需要(id)
强制转换。
Because gradient.colors
expects a NSArray
of CGColorRef
. The (id)
cast is also needed in order to create the NSArray
.
这篇关于UIView上的绘图渐变不适用于iOS 9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文