Swift:UICollectionView Cell在实现sizeForItemAtIndexPath时无法更新帧

时间:2017-06-12 13:57:53

标签: ios swift3 uicollectionview uicollectionviewcell

当我实现以下代码时:

dynamic var predicate: NSPredicate? = {
    return NSPredicate { object, _ in
        (object as? MyClass)?.result
    }
}()

通过使用sizeForItemAt方法,我可以绘制我想要的帧,但是我的自定义collectionview单元格无法更新,我错过了哪里,请帮助我!!

my custom cell

debug view

2 个答案:

答案 0 :(得分:0)

i fix it by this ref UICollectionView cell subviews do not resize

thanks guys , at frist still have problem with my Cell.imageview , can not shape-round as i want , and i create a class for imageview ,below:

class CellImageView: UIImageView {
override func layoutSubviews() {
    super.layoutSubviews()
    let radius: CGFloat = self.bounds.size.width / 2.0
    self.layer.cornerRadius = radius
    self.clipsToBounds = true

}

and anything work perfectly

答案 1 :(得分:0)

在iOS 13中,对于我来说,我先将estimatedItemSize固定为.zero,然后将itemSize设置为;

if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
 layout.estimatedItemSize = .zero
 layout.itemSize = CGSize(width: collectionView.frame.size.width, height: 80)
}