ios - 使用自定义颜色设置渐变

标签 ios view colors gradient

我正在尝试使用自定义颜色在我的主视图上设置渐变。虽然 View 显示为完全白色。这个记录有什么问题?

 CAGradientLayer *gradient = [CAGradientLayer new];

 gradient.colors = @[(id)[UIColor colorWithRed:92.0 green:196.0 blue:244.0 alpha:1].CGColor,
                    (id)[UIColor colorWithRed:47.0 green:146.0 blue:229.0 alpha:1].CGColor];
gradient.frame = self.backGroundView.bounds;
gradient.locations = @[@0.5, @0.5];

[self.backGroundView.layer insertSublayer:gradient atIndex:0];

这是正确显示的,蓝色和红色切割了 View 高度的一半。

CAGradientLayer *gradient = [CAGradientLayer new];
gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor];
gradient.frame = self.backGroundView.bounds;
gradient.locations = @[@0.5, @0.5];

[self.backGroundView.layer insertSublayer:gradient atIndex:0];

最佳答案

你的颜色不对。 UIColor 初始值设定项需要介于 0 和 1 之间的值。试试这个:

gradient.colors = @[(id)[UIColor colorWithRed:92/255.0 green:196/255.0 blue:244/255.0 alpha:1].CGColor,
                    (id)[UIColor colorWithRed:47/255.0 green:146/255.0 blue:229/255.0 alpha:1].CGColor];

关于ios - 使用自定义颜色设置渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43032972/

相关文章:

ios - 核心数据重复实体

iphone - SDWebImage清除缓存

iOS 自定义 UIButton 未出现在 View 中

java - 反射没有正确反射

d3.js - 如何在D3折线图中自定义色阶?

ios - Swift - 获取国家列表 -> fatal error : Can't unwrap Optional. 无

ios - 适用于 iOS 的 AdMob 错误 : Llibrary not found for -lGoogleAdMobAds

ios - 从本地通知打开应用程序后动画标签 [Swift 3.0 - Xcode 8]

SQL View : Finding maximum volume based per day

html - 动态改变颜色的菜单