Swift UICollectionReusableView 在重用时没有更新框架

问题描述

我尝试使用与装饰项相同的 UICollectionReusableView 构建多个部分。但是当视图在滚动过程中被重用时,ReusableView 内部的gradientLayer 没有更新以满足新的部分大小。我不知道有什么问题。重用 UICollectionReusableView 时如何重置渐变层框架。

class RoundedBackgroundView: UICollectionReusableView {

    private var insetView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.layer.cornerRadius = 6
        view.clipsToBounds = true
        return view
    }()


    override init(frame: CGRect) {
        super.init(frame: frame)
        let gradientLayer = CAGradientLayer()
        // Set the size of the layer to be equal to size of the display.
        gradientLayer.frame = self.bounds
        // Set an array of Core Graphics colors (.cgColor) to create the gradient.
        gradientLayer.colors = [UIColor(red: 246,green: 186,blue: 58).cgColor,UIColor(red: 233,green: 115,blue: 58).cgColor]
        gradientLayer.startPoint = CGPoint(x: 0.0,y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1.0,y: 0.5)
        // Rasterize this static layer to improve app performance.
        gradientLayer.shouldRasterize = true
        insetView.layer.addSublayer(gradientLayer)
        addSubview(insetView)

        NSLayoutConstraint.activate([
            insetView.leadingAnchor.constraint(equalTo: leadingAnchor,constant: 0),insetView.trailingAnchor.constraint(equalTo: trailingAnchor,insetView.topAnchor.constraint(equalTo: topAnchor),insetView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)