我有一个CollectionViewViewController
,它是从另一个推入的。 CollectionViewViewController
的视图有两个子视图:内部带有标签的简单UIView和UICollectionView
。首次展示时,所有功能都可以正常工作,并且渐变显示正确。但是,当我弹出该View Controller(因此它已被释放)并再次从同一父View Controlle推入它时,没有显示渐变。
像下面这样使单元出队:
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 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个子层,但是我确定我正在清除子层数组,所以我认为这不是问题。
答案 0 :(得分:0)
不应调用layoutSubviews(),应用程序会在需要时自行调用此func(这就是为什么在开始时被两次调用的原因) 如Swift Developer网站所述: https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews
您不应直接调用此方法。如果要强制更新布局,请在下次下一次图形更新之前调用setNeedsLayout()方法。如果要立即更新视图的布局,请调用layoutIfNeeded()方法。
尝试避免使用或调用layoutSubviews(),而是可以尝试在func中设置gradientLayer变量,并在需要调用它时随时调用它,请在setupView()下尝试
让我知道是否有帮助。