简体   繁体   English

将渐变层添加到uiviewcontroller uiview

[英]Add gradient layer to uiviewcontroller uiview

I am trying to create a simple gradient for the background of a UIViewController. 我正在尝试为UIViewController的背景创建一个简单的渐变。 My code is: 我的代码是:

    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = view.bounds
    let bottomColor = UIColor(hue: 208 / 360, saturation: 82 / 100, brightness: 0.9, alpha: 1)
    let topColor = UIColor(hue: 208 / 360, saturation: 41 / 100, brightness: 0.9, alpha: 1)
    gradientLayer.colors = [bottomColor, topColor]
    gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.1)
    gradientLayer.endPoint = CGPoint(x: 0.5, y: 0.0)

    view.layer.addSublayer(gradientLayer)

The gradient does not show. 渐变不显示。 When I look in the view debugger, I do not see it. 当我查看视图调试器时,看不到它。 I check the frame of the view and it looks ok 我检查了视图的框架,看起来还不错

x: 0, y:0, width: 375, height: 667

I've tried the view.layer.insert at method as well, but that did not work. 我也在方法上尝试过view.layer.insert ,但这没有用。 If I do 如果我做

view.layer.backgroundColor.orange

then I do see an orange background. 然后我确实看到了橙色背景。 Am I missing something? 我想念什么吗?

Change 更改

gradientLayer.colors = [bottomColor, topColor]

to

gradientLayer.colors = [bottomColor.cgColor, topColor.cgColor]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM