'Adjust insets of a specific section in UICollectionView

  1. insetForSectionAtIndex (on DelegateFlowLayout) enables one to set insets for all cells within a section

  2. sectionInset (on FlowLayout) enables one to set insets that applies to all sections.

However, I am looking for a way of applying insets to only one specific section - is this possible?



Solution 1:[1]

for swift 4 is named:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
...

Solution 2:[2]

You'd do something like this from the UICollectionViewDelegateFlowLayout protocol:

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets {

        switch section {
        case 0:
            return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        case 1:
            return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ingconti
Solution 2 JaredH