Recently, I\'m trying to do some projects for practicing. So I watched the course \"Developing Apps for iOS\", Stanford University, CS193P, 2017.
And now, I\'m doing
Provide
estimatedSize
to yourUICollectionViewLayout
.The estimated size should be the size shown in the size inspector of your xib.
collectionViewLayout.estimatedItemSize = CGSize(width: collectionView.frame.width, height: 50)
Override the method
preferredLayoutAttributesFitting(_:)
in yourUICollectionViewCell
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
setNeedsLayout()
layoutIfNeeded()
let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
var frame = layoutAttributes.frame
frame.size.height = ceil(size.height)
layoutAttributes.frame = frame
return layoutAttributes
}
Your collection view cell will now have dynamic size as per the content.