gpt4 book ai didi

ios - 无法将渐变添加到 UIViewController 中的 UIImageView

转载 作者:搜寻专家 更新时间:2023-11-01 06:49:28 25 4
gpt4 key购买 nike

UICollectionViewCell 中,我向 UIImageView 添加渐变,如下所示:

    override func layoutSubviews() {
super.layoutSubviews()

if thumbnailImageView.layer.sublayers?.first == nil {
thumbnailImageView.addGradient(firstColor: .clear, secondColor: .black)
}
}

我想做同样的事情,但是在 UIViewController 中,到目前为止我只能让它在 viewDidAppear 上工作

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

if thumbnailImageView.layer.sublayers?.first == nil {
thumbnailImageView.addGradient(firstColor: .clear, secondColor: .black)
}
}

我想让 UIImageView 像我在 UICollectionView 中那样显示渐变。你知道我该怎么做吗?

extension UIView{

func addGradient(firstColor: UIColor, secondColor: UIColor){
clipsToBounds = true
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [firstColor.cgColor, secondColor.cgColor]
gradientLayer.frame = self.bounds
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 0, y: 1)
self.layer.insertSublayer(gradientLayer, at: 0)
}
}

最佳答案

可以在viewDidLayoutSubviews中调用

关于ios - 无法将渐变添加到 UIViewController 中的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58340224/

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