1. Compositonal Layout 구성

Untitled

1-1. Compositional Layout은 Item, Group, Section 으로 구성되어 있습니다.

1-2. NSCollectionLayoutDimension — Layout Size를 지정할 때 사용

private func createLayout() -> UICollectionViewLayout {
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.2),
                                              heightDimension: .fractionalHeight(1.0))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        item.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 12)

        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                               heightDimension: .fractionalWidth(0.2))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
        
        let section = NSCollectionLayoutSection(group: group)
				section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 14, bottom: 16, trailing: 14)
        
        let layout = UICollectionViewCompositionalLayout(section: section)
        
        return layout
    }

1-3. 스크롤 되게 하려면

1-4. Supplementary and decoration view

let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(44))
let headerSupplementary = NSCollectionLayoutBoundarySupplementaryItem(
  layoutSize: headerSize,
  elementKind: ViewController.elementKindSectionHeader,
  alignment: .top)
section.boundarySupplementaryItems = [headerSupplementary]
section.supplementariesFollowContentInsets = false

// Background
let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(elementKind: ViewController.sectionBackgroundDecorationElementKind)
section.decorationItems = [sectionBackgroundDecoration]

// Register decoration view
let layout = UICollectionViewCompositionalLayout(section: section)
layout.register(
    SectionBackgroundDecorationView.self,
    forDecorationViewOfKind: ViewController.sectionBackgroundDecorationElementKind)