将渐变图层添加到uiviewcontroller uiview

时间:2018-02-04 23:21:45

标签: ios uiview uiviewcontroller

我正在尝试为UIViewController的背景创建一个简单的渐变。我的代码是:

    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)

渐变未显示。当我查看视图调试器时,我没有看到它。我检查了视图的框架,看起来没问题

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

我也尝试了view.layer.insert方法,但这不起作用。如果我做

view.layer.backgroundColor.orange

然后我看到橙色背景。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

更改

gradientLayer.colors = [bottomColor, topColor]

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