Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 448
Gaurav Sharma Avatar asked Aug 09 '16 18:08

Gaurav Sharma


People also ask

What is Section inset?

Section insets are margins applied only to the items in the section. They represent the distance between the header view and the first line of items and between the last line of items and the footer view. They also indicate the spacing on either side of a single line of items.

What is UICollectionView flow layout?

A layout object that organizes items into a grid with optional header and footer views for each section.

What is Collectionviewlayout?

In essence, UICollectionViewLayout is just a class object that takes the responsibility of arranging the child views of a UICollectionView. But in UICollectionView world, we call the child views Cell and we set the views through a wrapper class called UICollectionViewLayoutAttributes.


1 Answers

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;

}
like image 110
pableiros Avatar answered Oct 02 '22 15:10

pableiros