OStack程序员社区-中国程序员成长平台

标题: ios - 将渐变层添加到 uiviewcontroller uiview [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:42
标题: ios - 将渐变层添加到 uiviewcontroller uiview

我正在尝试为 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)

渐变不显示。当我查看 View 调试器时,我看不到它。我检查了 View 的框架,它看起来不错

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

我也尝试过 view.layer.insert at 方法,但没有奏效。如果我这样做了

view.layer.backgroundColor.orange

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



Best Answer-推荐答案


改变

gradientLayer.colors = [bottomColor, topColor]

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

关于ios - 将渐变层添加到 uiviewcontroller uiview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48614012/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4