I am using UICollectionViewDelegateFlowLayout
to display cells of dynamic height in my Collection View. The size of the cells are set in sizeForItemAt
.
Goal: Each Collection View cell has a toggle function that should change the dynamically-calculated height to 50%. This is where I am running into problems.
In sizeForItemAt
I am getting the dynamic height.
var dynamicHeight = (header + content + footer)
return CGSize(width: self.view.frame.width, height: dynamicHeight)
I'd like to update sizeForItemAt
to something like the following, when a button on the cell gets pressed:
return CGSize(width: self.view.frame.width, height: (dynamicHeight / 2))
How do I change the height of a Collection View Cell while updating sizeForItemAt
?
In order to achieve what you want you need to:
collectionView(_:layout:sizeForItemAt:)
function can access it easily using the index path andreloadItems(at:)
function of the collection view, passing the cell's index path, to update the cell's size.You can see an example in the following code: