gpt4 book ai didi

ios - 如何在运行时更改渐变层的框架?

转载 作者:搜寻专家 更新时间:2023-10-31 22:24:05 32 4
gpt4 key购买 nike

当我运行应用程序时,渐变层框架与 UIView 框架不匹配。我尝试使用以下代码:

let color1  =  UIColor(red: 20.0/255, green: 43.0/255, blue: 81.0/255, alpha: 1.0).cgColor
let color2 = UIColor(red: 149.0/255, green: 45.0/255, blue: 122.0/255, alpha: 1.0).cgColor

let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.backBarcodeSrch.bounds
gradientLayer.colors = [color1,color2 ]
gradientLayer.locations = [0.0, 1.0]

gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.6)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.6)
self.backBarcodeSrch.layer.addSublayer(gradientLayer)

最佳答案

ViewControllerviewDidLayoutSubviews() 中更新 gradientLayerframe

class VC: UIViewController {
var gradientLayer = CAGradientLayer()

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
gradientLayer.frame = self.backBarcodeSrch.bounds
}

override func viewDidLoad() {
super.viewDidLoad()
self.addGradient()
}

func addGradient() {
let color1 = UIColor(red: 20.0/255, green: 43.0/255, blue: 81.0/255, alpha: 1.0).cgColor
let color2 = UIColor(red: 149.0/255, green: 45.0/255, blue: 122.0/255, alpha: 1.0).cgColor

gradientLayer.frame = self.backBarcodeSrch.bounds
gradientLayer.colors = [color1,color2 ]
gradientLayer.locations = [0.0, 1.0]

gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.6)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.6)
self.backBarcodeSrch.layer.addSublayer(gradientLayer)
}
}

关于ios - 如何在运行时更改渐变层的框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642333/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com