Compositional Layout
CollectionView์์ ์ฌ์ฉํ๋ค. ๊ณตํต์ ์ธ cell์ ํํ๋ณด๋ค๋ ๋ค์ํ ์ฌ์ด์ฆ์ cell๋ค์ ํํํ ๋ ์ฌ์ฉํ๊ธฐ ์ข๋ค.
Compositional Layout์ CollectionView Layout ์ค ํ๋์ด๋ค. (FlowLayout ๊ณผ ๋น์ทํ๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.)
์์ ๊ฐ์ด item, group, section์ ์ง์ ํด์ ๋ค์ํ ํํ์ cell๋ค์ ์ฝ๊ฒ ๋ํ๋ผ ์ ์๋ค.
Section ์์ Group, Group ์์ Item, Item์ Cell ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ํธํ๋ค
๊ธฐ๋ณธ์ ์ธ Compositional Layout ์ฝ๋
func createBasicListLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(44))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,
subitems: [item])
let section = NSCollectionLayoutSection(group: group)
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
NSCollectionLayoutSize
item์ height์ width๋ฅผ ๋ํ๋ด๋ ๋ฐฉ๋ฒ์ด๋ค.
1. absolute (CGFloat)
๋ง ๊ทธ๋๋ก ์ ๋ ํฌ๊ธฐ์ด๋ค. ํญ์ ๊ณ ์ ๋ ํฌ๊ธฐ๋ก ๋ํ๋๋ค.
let absoluteSize = NSCollectionLayoutSize(widthDimension: .absolute(44), heightDimension: .absolute(44))
2. estimated(CGFloat)
๋ฐํ์์ ํฌ๊ธฐ๊ฐ ๋ณํ ๊ฐ๋ฅ์ฑ์ด ์๋ ๊ฒฝ์ฐ estimated๋ฅผ ์ฌ์ฉํ๋ค.
์์คํ ์ด ์์ ํฌ๊ธฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ค์ ํฌ๊ธฐ๋ฅผ ๊ณ์ฐํ๋ค.
let estimatedSize = NSCollectionLayoutSize(widthDimension: .estimated(200), heightDimension: .estimated(100))
3. fractionalWidth(CGFloat), fractionalHeight(CGFloat)
ํ์ฌ ์์ ์ด ์ํ ์ปจํ ์ด๋์ ํฌ๊ธฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋น์จ๋ก์จ ์์ ์ ํฌ๊ธฐ๋ฅผ ์ ํ๋ค.
0.0~1.0 ์ฌ์ด์ CGFloat๊ฐ์ ๋ฃ์ ์ ์๋ค.
let fractionalSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.2), heightDimension: .fractionalWidth(0.2))
sectionNumber์ ๋ฐ๋ฅธ section ์์ฑ
static func create() -> UICollectionViewCompositionalLayout {
return UICollectionViewCompositionalLayout { (sectionNumber, _ environment) -> NSCollectionLayoutSection? in
let section: NSCollectionLayoutSection
switch sectionNumber {
case 0:
section = createMainSection()
case 1:
section = createUserClipSection()
case 2:
section = createWeeklyLinkSection()
case 3:
section = createWeeklyRecommendSection()
default:
section = createMainSection()
}
return section
}
}
item, group, section ์ก๊ธฐ
static func createUserClipSection() -> NSCollectionLayoutSection {
let itemFractionalWidthFraction = 1.0 / 2.0
let itemInset: CGFloat = 7
// item
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(itemFractionalWidthFraction),
heightDimension: .absolute(150))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets = NSDirectionalEdgeInsets(top: itemInset, leading: itemInset, bottom: itemInset, trailing: itemInset)
// group
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),
heightDimension: .absolute(268))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
// section
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: itemInset, leading: itemInset, bottom: itemInset, trailing: itemInset)
return section
}
Header, Footer ์ก๊ธฐ
// Header, Footer
section.boundarySupplementaryItems = [
NSCollectionLayoutBoundarySupplementaryItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .top),
NSCollectionLayoutBoundarySupplementaryItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(20)), elementKind: UICollectionView.elementKindSectionFooter, alignment: .bottom)
]
item๋ค ์ฌ์ด์ ๊ฐ๊ฒฉ ๋์ฐ๊ธฐ
let itemInset: CGFloat = 0
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(itemFractionalWidthFraction),
heightDimension: .fractionalHeight(1))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets = NSDirectionalEdgeInsets(top: itemInset,
leading: itemInset,
bottom: itemInset,
trailing: itemInset)