Set insets to the CollectionView programmatically

2019-06-15 19:03发布

问题:

I have ViewController with the CollectionView in it. I want to change CollectionView insets by pressing button. What code I should implement?

Thank you!

回答1:

Set the section inset and invalidate the layout.

let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout

collectionViewLayout?.sectionInset = ... // some UIEdgeInset

collectionViewLayout?.invalidateLayout()