7
  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?

Gaurav Sharma
  • 2,680
  • 3
  • 26
  • 36

3 Answers3

9

You must have to implement UICollectionViewDelegateFlowLayout to your class with this method:

For Swift 4, 5+

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

    var edgeInsets = UIEdgeInsets()
    if section == THE_SECTION_YOU_WANT {
        // edgeInsets configuration stuff
    }

    return edgeInsets;
}

For Swift 3

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

    var edgeInsets = UIEdgeInsets()
    if section == THE_SECTION_YOU_WANT {
        // edgeInsets configuration stuff
    }

    return edgeInsets;

}
pableiros
  • 14,932
  • 12
  • 99
  • 105
  • I tried this earlier, and it seems to apply the inset to all elements in the section. Is there a way I can apply it to the entire section, not each element? – Gaurav Sharma Aug 10 '16 at 13:48
  • @GauravSharma This method apply the inset to the entire section, that's the reason it gives it an section `Int`. – pableiros Aug 10 '16 at 14:28
  • just tried again - same result - it applies the insets to each cell in the section, rather than the section. – Gaurav Sharma Aug 10 '16 at 16:42
  • @GauravSharma and what is the difference? Do you want to apply the inset to the header section and footer section? – pableiros Aug 10 '16 at 16:53
  • Nope, I want to inset the entire section (not each item in the section). – Gaurav Sharma Aug 10 '16 at 17:00
  • Actually that works fine - there was something else that made it look like it was not working. Putting a 100 pixel left/right inset works fine - if anyone else tries this and gets odd results, bear in mind the collection view applies automatic spacing which may make it look like inner cells are being inset! – Gaurav Sharma Aug 10 '16 at 18:21
1

for swift 4 is named:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
...
ingconti
  • 10,876
  • 3
  • 61
  • 48
1

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)
        }
    }
JaredH
  • 2,338
  • 1
  • 30
  • 40