我有一个从另一个推出的CollectionViewViewController
。这个CollectionViewViewController
的视图有两个子视图:简单的UIView标签内部和UICollectionView
。当它第一次呈现时,一切都很好,并且正确显示渐变。但是当我弹出这个视图控制器(因此它被解除分配)并从同一个父视图控件再次推送时,没有显示渐变。
该单元格如下出列:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PageScrollGeminiCellIdentifier, for: indexPath) as! PageScrollGeminiCell
let item = viewModel.items[indexPath.item]
cell.configure(withTitle: item.name, description: item. description, imageURL: item.imageUrl, gradientColor: item.color, theme: theme)
mainView.geminiView.animateCell(cell)
return cell
}
这是我在细胞类方法和细胞生命周期中所做的事情:
private var gradientColor: UIColor?
private var gradientLayer: CAGradientLayer?
[...]
override init(frame: CGRect) {
super.init(frame: .zero)
setupView()
}
required init?(coder aDecoder: NSCoder) {
return nil
}
override func layoutSubviews() {
if gradientLayer != nil {
gradientView.layer.sublayers = nil
}
if let color = gradientColor {
let gradientPoints = (CGPoint(x: 0.0, y: 0.15), CGPoint(x: 0.0, y: 1.0))
self.gradientLayer = gradientView.applyGradient(with: [color, color.withAlphaComponent(0.08)], gradient: .vertical(gradientPoints))
}
}
// MARK: Private
private func setupView() {
clipsToBounds = true
layer.cornerRadius = 15
backgroundColor = .clear
addSubview(containerView)
containerView.addSubview(backgroundImageView)
containerView.addSubview(gradientView)
containerView.addSubview(labelsContainerView)
labelsContainerView.addSubview(titleLabel)
labelsContainerView.addSubview(descriptionLabel)
containerView.snp.makeConstraints { maker in
maker.edges.equalToSuperview()
}
backgroundImageView.snp.makeConstraints { maker in
maker.edges.equalToSuperview()
}
gradientView.snp.makeConstraints { maker in
maker.leading.trailing.top.bottom.equalToSuperview()
}
labelsContainerView.snp.makeConstraints { maker in
maker.leading.top.equalTo(16)
maker.trailing.equalTo(-16)
}
titleLabel.snp.makeConstraints { maker in
maker.top.leading.trailing.equalToSuperview()
}
descriptionLabel.snp.makeConstraints { maker in
maker.top.equalTo(titleLabel.snp.bottom).offset(8)
maker.leading.trailing.bottom.equalToSuperview()
}
}
// MARK: - Internal
func configure(withTitle title: String?, description: String?, imageURL: String?, gradientColor: UIColor?, theme: Themeable) {
backgroundImageView.setImage(withString: imageURL)
titleLabel.text = title
titleLabel.font = theme.font.h2
descriptionLabel.text = description
descriptionLabel.font = theme.font.b1
self.gradientColor = gradientColor
}
另外我想告诉你applyGradient
func:
func applyGradient(with colours: [UIColor], gradient orientation: GradientOrientation) -> CAGradientLayer {
layoutSubviews()
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
gradient.cornerRadius = layer.cornerRadius
self.layer.insertSublayer(gradient, at: 0)
return gradient
}
我发现的是,当第一次呈现视图控制器时,layoutSubviews()方法被调用两次,但是每个下一次每个单元只被调用一次。
现在我不确定我是不是在我的单元格中插入了超过1个子图层,但我确定我正在清除子图层数组,所以我认为这不是问题所在。
不应该调用layoutSubviews(),app在需要时自己调用这个func(这就是为什么它在开始时被调用两次)如Swift Developer网站所述:https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews
您不应该直接调用此方法。如果要强制进行布局更新,请在下次绘图更新之前调用setNeedsLayout()方法。如果要立即更新视图的布局,请调用layoutIfNeeded()方法。
尝试避免使用或调用layoutSubviews(),而是可以尝试在func中设置gradientLayer变量并在需要调用时调用它,尝试下面的setupView()
如果有帮助,请告诉我。