Created
April 5, 2023 13:16
-
-
Save jsryudev/7e213334b6788c07074f5191e8e1cba9 to your computer and use it in GitHub Desktop.
Compositional.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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