Building Spotify App in Swift 5 & UIKit - Part 8 (Xcode 12, 2021, Swift 5) - Build App
private var collectionView: UICollectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewCompositionalLayout { sectionIndex, _ -> NSCollectionLayoutSection? in
return HomeViewController.createSectionLayout(section: sectionIndex)})
레이아웃을 통해 각 섹션 내 아이템의 수직/수평 방향 배열을 어떻게 할 것인지 결정할 수 있다! 물론 각 아이템 별 크기, 공백 등 다양하게 결정할 수 있는, "UICollectionView를 자유자재로 그리는 방법".
private static func createSectionLayout(section: Int) -> NSCollectionLayoutSection {
switch section {
case 0:
let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(1)))
item.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2)
// Group
let verticalGroup = NSCollectionLayoutGroup.vertical(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(390)
),
subitem: item,
count: 3)
let horizontalGroup = NSCollectionLayoutGroup.horizontal(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(0.9),
heightDimension: .absolute(390)
),
subitem: verticalGroup,
count: 1)
// Section
let section = NSCollectionLayoutSection(group: horizontalGroup)
section.orthogonalScrollingBehavior = .groupPaging
return section
case 1:
let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .absolute(200), heightDimension: .absolute(200)))
item.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2)
// Group
let verticalGroup = NSCollectionLayoutGroup.vertical(
layoutSize: NSCollectionLayoutSize(
widthDimension: .absolute(200),
heightDimension: .absolute(400)
),
subitem: item,
count: 2)
let horizontalGroup = NSCollectionLayoutGroup.horizontal(
layoutSize: NSCollectionLayoutSize(
widthDimension: .absolute(200),
heightDimension: .absolute(400)
),
subitem: verticalGroup,
count: 1)
// Section
let section = NSCollectionLayoutSection(group: horizontalGroup)
section.orthogonalScrollingBehavior = .continuous
return section
case 2:
let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0)))
item.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2)
// Group
let group = NSCollectionLayoutGroup.vertical(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .absolute(80)
),
subitem: item,
count: 1)
// Section
let section = NSCollectionLayoutSection(group: group)
return section
default:
let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(1)))
item.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2)
// Group
let group = NSCollectionLayoutGroup.vertical(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(390)
),
subitem: item,
count: 1)
// Section
let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .groupPaging
return section
}
}
switch-case
문 사용verticalGroup
verticalGroup
을 일종의 서브 아이템으로 취급하는 수평 그룹 horizontalGroup
등 이중 배치 가능UICollectionView의 레이아웃은 아이템, 아이템이 이루는 그룹, 그룹이 이루는 섹션을 통해 구성된다. 수평, 수직 등 방향 및 각 아이템과 그룹의 크기, 이동 방법에 따라 구성되는 뷰가 달라지기 떄문에 커스텀에 주의하자!