How to dynamically change the height of the section or group

How to dynamically change the height of the section or group when i use UICollectionViewCompositionalLayout.
For example, i want to change the first section's height when i scroll the collectionview up.

I can use the following code to achieve, but this looks very bad.

Code Block swift
// in didScroll callback
collectionView.setCollectionViewLayout(currentCollectionViewLayout(), animated: true)


Replies

If you want to request that the sizes of visible items are recomputed, you can do that without setting a new layout by simply calling collectionView.collectionViewLayout.invalidateLayout(). You might do this after directly updating the content inside of existing cells that are already visible, for example.

If you are trying to update the layout attributes of items on each frame during scrolling, however, you probably want to use a different approach. You can use the visibleItemsInvalidationHandler on NSCollectionLayoutSection to make adjustments to the layout attributes (e.g. size, transform, etc) for each of the visible items. Read more about this in the documentation.

The best approach depends on what exactly you are trying to achieve, but those are two options to explore. Feel free to share more details about what you are trying to do if you need more guidance.
visibleItemsInvalidationHandler sounds like it’s got SOOOO much potential for amazing animations and layouts.
Are there any code examples? Asking for a friend. 😜