I have an override function for my collection view:
// Sets a the width as the screen width and has a dynamic height that changes depending on how much is inside the cell.
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
let targetSize = CGSize(width: layoutAttributes.frame.width, height: 40)
layoutAttributes.frame.size = contentView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel)
print("Layout has been changed.")
return layoutAttributes
}
This function makes the cell heights dynamic depending on the height of the contents inside the cell while keeping the width a static width of the phone screen. This function works exactly how I want it to work.
The problem that I am having is this function is only called when the view loads, and I want it to also run when I call collectionView.reloadData()
. So when I call reloadData()
the cell goes off of its estimatedItemSize
instead of its size from the function above preferredLayoutAttributes
.
Does anyone know how I can allow this function to run when I call collectionView.reloadData()
?