UICollectionViewDelegateFlowLayout没有改变细胞界限

问题描述 投票:0回答:1

我有设置UICollectionViewCell其具有相对宽度来collectionView边界阴影路径问题。

即时通讯使用的故事板的约束,在AwakeFromNib方法建立影子,并用sizeForItemAt方法调整细胞

 //cell awakeFromNib    
    override func awakeFromNib() {

    self.backgroundColor = UIColor.white

    self.contentView.layer.cornerRadius = 2.0
    self.contentView.layer.borderWidth = 1.0
    self.contentView.layer.borderColor = UIColor.clear.cgColor
    self.contentView.layer.masksToBounds = true

    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOffset = CGSize(width: 0, height: 2.0)
    self.layer.shadowRadius = 2.0
    self.layer.shadowOpacity = 0.5
    self.layer.masksToBounds = false
    self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.contentView.layer.cornerRadius).cgPath
}



// collection view method
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let height: CGFloat = 288

    return CGSize(width: collectionView.bounds.size.width-20, height: height)
}

细胞界限:

AwakeFromNib方法 - (307.0,288.0)

预计 - (300.0,288.0)

问题是什么?

swift uicollectionview uicollectionviewcell uicollectionviewdelegateflowlayout
1个回答
0
投票

问题是,如果你的电池layouted边界/框架正在发生变化。

添加到您的collectionViewCell - 子类:

    override func layoutSubviews() {
    super.layoutSubviews()
        self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds,  cornerRadius: self.contentView.layer.cornerRadius).cgPath
    }
© www.soinside.com 2019 - 2024. All rights reserved.