Skip to content

Instantly share code, notes, and snippets.

@jsryudev
Created April 5, 2023 13:16
Show Gist options
  • Save jsryudev/7e213334b6788c07074f5191e8e1cba9 to your computer and use it in GitHub Desktop.
Save jsryudev/7e213334b6788c07074f5191e8e1cba9 to your computer and use it in GitHub Desktop.
Compositional.swift
enum Section: Int {
case name
case newFeature
case preview
}
func compositionLayout() -> UICollectionViewLayout {
let layout = UICollectionViewCompositionalLayout { index, environment in
guard let section = Section(rawValue: index) else { return nil }
let sectionLayout: NSCollectionLayoutSection
switch section {
case .name:
let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(200)
)
let item = NSCollectionLayoutItem(
layoutSize: itemSize
)
let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(100)
)
let group = NSCollectionLayoutGroup.vertical(
layoutSize: groupSize,
subitems: [item]
)
let headerSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(44)
)
let header = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: headerSize,
elementKind: UICollectionView.elementKindSectionHeader,
alignment: .top
)
let section = NSCollectionLayoutSection(group: group)
section.boundarySupplementaryItems = [header]
sectionLayout = section
case .newFeature:
let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(120)
)
let item = NSCollectionLayoutItem(
layoutSize: itemSize
)
let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(100)
)
let group = NSCollectionLayoutGroup.vertical(
layoutSize: groupSize,
subitems: [item]
)
let headerSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(44)
)
let header = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: headerSize,
elementKind: UICollectionView.elementKindSectionHeader,
alignment: .top
)
let section = NSCollectionLayoutSection(group: group)
section.boundarySupplementaryItems = [header]
sectionLayout = section
case .preview:
let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(600)
)
let item = NSCollectionLayoutItem(
layoutSize: itemSize
)
let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(0.56),
heightDimension: .fractionalWidth(1.0)
)
let group = NSCollectionLayoutGroup.horizontal(
layoutSize: groupSize,
subitems: [item]
)
let headerSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(44)
)
let header = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: headerSize,
elementKind: UICollectionView.elementKindSectionHeader,
alignment: .top
)
let section = NSCollectionLayoutSection(group: group)
section.boundarySupplementaryItems = [header]
section.orthogonalScrollingBehavior = .continuous
sectionLayout = section
}
return sectionLayout
}
return layout
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment