Modern UIKit Collection, TableView #2-2
Advances in Collection View Layout #2
Advances in Collection View Layout - WWDC19 - Videos - Apple Developer
Collection View Layouts make it easy to build rich interactive collections. Learn how to make dynamic and responsive layouts that range...
developer.apple.com
๐ก Collection View Layout์ ์ฌ์ฉํ๋ฉด ํ๋ถํ interactive ์ปฌ๋ ์ ์ ์ฝ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค. ๊ธฐ๋ณธ ๋ชฉ๋ก๋ถํฐ ๋ค์ฐจ์ ํ์ ํ๊ฒฝ๊น์ง ๋์ ์ด๊ณ ๋ฐ์์ด ๋น ๋ฅธ ๋ ์ด์์์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์์๋ณด์ธ์.
CompositonalLayout ์ฃผ์ ์ปดํฌ๋ํธ
1. NSCollectionLayoutSize
→ Item๊ณผ Group ์ปดํฌ๋ํธ์์ ์ฌ์ฉํจ
2. NSCollectionLayoutItem
์ปฌ๋ ์ ๋ทฐ ๋ ์ด์์์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์ฑ ์์์ ๋๋ค.
⇒ ์ฃผ์ Property
- supplementaryItems → ๊ฐ๊ฐ์ ์์ดํ ์ ์ถ๊ฐ๋ก ๋ง๋ถ์ด๋ ์์ดํ
- edgeSpacing → ์์ดํ ๋ค ์ฌ์ด์ ๋ฃ๋ ๊ฐ๊ฒฉ
- contentInsets → ์์ดํ ๋ด๋ถ์์ ๊ฐ์ ๊ฐ๊ฒฉ
โ Item์ supplementaryItem๋ค์ ๊ฐ์ง ์ ์๋ค.
3. NSCollectionLayoutGroup
๊ฒฝ๋ก๋ฅผ ๋ฐ๋ผ ํญ๋ชฉ์ ๋ฐฐ์นํ๋ ํญ๋ชฉ ์งํฉ์ ์ํ ์ปจํ ์ด๋์ ๋๋ค.
โ ๏ธ NSCollectionLayoutItem์ ์์ ๋ฐ๋๋ค!!
⇒ ์ฃผ์ Property
- subitems → ๊ทธ๋ฃน์ ๊ตฌ์ฑํ Item ๋ฐฐ์ด, ์์ Group์ Item ๋ฐฐ์ด ๋ฐฐ์น๋ฅผ ์ด๋ป๊ฒ ํด์ผํ ์ง ์ ์ธํด์ผํ๋ค.
- interItemSpacing → ์์ดํ ๊ฐ์ ๊ฐ๊ฒฉ์ ์ด๋ป๊ฒ ํ ์ง ์ค์ ํ๋ค.
⇒ ์์ฑ ๋ฐฉ๋ฒ - 3๊ฐ์ง ํ์ ์์ฑ์ผ๋ก ํธ์ถํ๊ธฐ
- ํ ๋ฐฐ์ด์ผ๋ก ์์ฑํ๊ธฐ
- ์ด ๋ฐฐ์ด๋ก ์์ฑํ๊ธฐ
- ์ปค์คํ
๊ทธ๋ฃน ์์ฑํ๊ธฐ
→์ง์ ๊ทธ๋ฃน ๋ด๋ถ ์์ญ์ ์์ดํ ๋ค์ ์์น๋ฅผ ์ค์ ํ ์ ์๋ค… ๊ทธ๋ฌ๋ ์ ์ฌ์ฉ ์ํ๋ ๋ฏ…
โ Group๋ NSCollectionLayoutItem์ ๊ฐ๊ธฐ ๋๋ฌธ์ Group์ subItem์ Group์ธ์คํด์ค๋ฅผ ๋ฃ์ด๋ ๋๋ค. ์ด๋ฐ ๊ธฐ๋ฒ์ Nested group์ด๋ผ ๋ถ๋ฅธ๋ค.
4. NSCollectionLayoutSection
๊ทธ๋ฃน ์งํฉ์ ๋ณ๊ฐ์ ์๊ฐ์ ๊ทธ๋ฃน์ผ๋ก ๊ฒฐํฉํ๋ ์ปจํ ์ด๋์ ๋๋ค.
⇒ ์์ฑํ๊ธฐ
- .init(group: NSCollectionLayoutGroup) → ๊ทธ๋ฃน์ ๋ฃ์ด์ ์์ฑ
- .list(using:
[UICollectionLayoutListConfiguration]
,
layoutEnvironment:[NSCollectionLayoutEnvironment]
) ⇒ List ํ์์ Section ์์ฑ
โ WWDC 20 ์ดํ์ ๋์จ ๊ฐ๋ : Lists in UICollectionView ์์ ๋ฌด์์ธ์ง ํ์ธํ๊ธฐ
⇒ ์ฃผ์ ์์ฑ
- ์น์
์คํฌ๋กค ๊ตฌ์ฒดํํ๊ธฐ
- orthogonalScrollingBehavior:
[UICollectionLayoutSectionOrthogonalScrollingBehavior]
⇒ ์ด Section์ ๊ฐ๋ CollectionView์ ์คํฌ๋กค ๋ฐฉํฅ๊ณผ ์์ง ๋ฐฉํฅ์ ์คํฌ๋กค์ ์ ์
- orthogonalScrollingBehavior:
- ์น์
Spacing ๊ตฌ์ฑํ๊ธฐ
- interGroupSpacing → ๊ทธ๋ฃน๋ค ๊ฐ์ Spacing ์ค์
- contentInsets → Section ๋ด๋ถ์ ๊ทธ boundary(header, footer)๊ฐ์ ๊ฐ๊ฒฉ ์ค์
- ์ถ๊ฐ์ ์ธ ๋ทฐ๋ค ๊ตฌ์ฑํ๊ธฐ
- boundarySupplementaryItems → Header,Footer๋ฅผ ์ค์ ํ ์ ์๋ค.
- decorationItems → Section์์ญ์ backgroundView๋ค์ ์ค์ ํ ์ ์๋ค.
โ Section์ boundarySupplyItem๊ณผ decorationItem์ ๊ฐ๋๋ค.
5. NSCollectionViewCompositionalLayout
์ ์ฐํ ์๊ฐ์ ๋ฐฐ์ด๋ก ํญ๋ชฉ์ ๊ฒฐํฉํ ์ ์๋ ๋ ์ด์์ ๊ฐ์ฒด์ ๋๋ค.
⇒ ์์ฑํ๊ธฐ
- ๋จ์ผ Section ์ปดํฌ๋ํธ๋ก ์กด์ฌํ๋ CollectionView ๋ง๋ค๊ธฐ
- NSCollectionViewCompositionalLayout(section: section)
- ์ฌ๋ฌ Section ์ปดํฌ๋ํธ๋ก ์กด์ฌํ๋ CollectionView ๋ง๋ค๊ธฐ
- NSCollectionViewCompositionalLayout(sectionProvider: NSCollectionViewCompositionalLayoutSectionProvider)
โ 1. Configuration์ผ๋ก ์ถ๊ฐ์ ์ธ Section๋ค ์ค์ ํ๊ธฐ ์ํด "NSCollectionViewCompositionalLayoutConfiguration" ํ์ธํ๊ธฐ
+ 2. ์ฌ๋ฌ Section์ผ๋ก ๊ตฌ์ฑํ๊ธฐ ์ํ "NSCollectionViewCompositionalLayoutSectionProvider" ํ์ธํ๊ธฐ
CompositonalLayout ๊ตฌ์ฑ ์ปดํฌ๋ํธ
1. Size ์ค์ ํ๊ธฐ
NSCollectionLayoutDimension
์ปฌ๋ ์ ๋ทฐ์์ ํญ๋ชฉ์ ๋๋น ๋๋ ๋์ด๋ฅผ ๋ํ๋ด๋ ๊ฐ๋ณ์ ์ธ ์์ญ์ ๋๋ค.
→ NSCollectionLayoutSize๋ฅผ ๊ตฌ์ฑํ๋ค.
๐ก CollectionView์์ ๋ํ๋ผ ๊ฐ๊ฐ์ ํญ๋ชฉ(Item, Group, Section ๋ฑ๋ฑ…)์์์ ๊ณ ์ ๋์ด ๋ฐ ๋๋น๊ฐ์ ์ง์ ํ๋ ์ญํ
⇒ 3๊ฐ์ง์ ํํ๋ฒ์ผ๋ก ๋๋
⇒ ํํ๋ฒ 3๊ฐ์ง
- Absolute(CGFloat) → ๊ณ ์ ํ point์ ํฌ๊ธฐ๋ก ์ค์ ํ๋ค.
- Estimated(CGFloat) → ์ํ๋ ํฌ๊ธฐ๋ฅผ ์
๋ ฅํ๋ฉด ๋ฐํ์์์ ์ค์ ์ปฌ๋ ์
๋ทฐ์์์ ํฌ๊ธฐ์ ๋ง๊ฒ ์ค์ ํ๋ค.
⇒ ํ์ํฌ์ธํธ ์๋ ๋ง์ถค ๊ธฐ๋ฅ๊ณผ ๋น์ท - fractionalHeight(CGFloat) - fractionalWidth(CGFloat) → ์ด ํญ๋ชฉ์ ์์ ๊ทธ๋ฃน์ ํฌ๊ธฐ์ ๋ง๊ฒ ์ค์ ํ๋ค.
2. Supplymentary ์ค์ ํ๊ธฐ
NSCollectionLayoutSupplementaryItem
์ปฌ๋ ์ ๋ณด๊ธฐ์์ ํญ๋ชฉ์ ์๊ฐ์ ์ฅ์์ ์ถ๊ฐํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ ๋๋ค.
๐ก ๋ณด์กฐ ํญ๋ชฉ์ ์ฌ์ฉํ์ฌ ์ฝํ ์ธ ์ ์ถ๊ฐ ๋ณด๊ธฐ๋ฅผ ์ฒจ๋ถํ ์ ์์ต๋๋ค.
+ ์๋ฅผ ๋ค์ด, ํญ๋ชฉ์ ๋ฐฐ์ง๋ฅผ ์ฒจ๋ถํ๊ฑฐ๋ ๊ทธ๋ฃน ์ฃผ์์ ํ๋ ์์ ์ฒจ๋ถํ ์ ์์ต๋๋ค.
+ ๋ณด์กฐ ํญ๋ชฉ์ ์ฒจ๋ถ๋ ํญ๋ชฉ์ ์ธ๋ฑ์ค ๊ฒฝ๋ก๋ฅผ ๋ฐ๋ฆ ๋๋ค.
⇒ ์ฃผ์ Property
- containerAnchor → Item๊ณผ ๊ทธ ์์ Container์์ ๊ฒฝ๊ณ์์ ์ค์ ํ ์์น
- itemAnchor → ์์ดํ ์ supplementaryItem์ ๋ฃ์ ๊ฒฝ์ฐ ์ค์ ํ ์์น
- elementKind → ์ด ์์ดํ ์ ๊ณ ์ ์์ด๋ (String)
- zIndex → ์์ดํ ๋ค ๊ฐ์ ์คํ ์์
NSCollectionLayoutBoundarySupplementaryItem
์ปฌ๋ ์ ๋ณด๊ธฐ์ header ๋๋ footer๋ฅผ ์ถ๊ฐํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ ๋๋ค.
⇒ NSCollectionLayoutSupplementaryItem
์ ์์ ํด๋์ค
๐ก ๊ฒฝ๊ณ ์ถ๊ฐ ํญ๋ชฉ์ ํน์ํ ์ ํ์ ์ถ๊ฐ ํญ๋ชฉ(NSCollectionLayoutSupplementaryItem)์ ๋๋ค. ๊ฒฝ๊ณ ๋ณด์กฐ ํญ๋ชฉ์ ์ฌ์ฉํ์ฌ ์ปฌ๋ ์ ๋ณด๊ธฐ์ ์น์ ๋๋ ์ ์ฒด ์ปฌ๋ ์ ๋ณด๊ธฐ์ ๋จธ๋ฆฌ๊ธ ๋๋ ๋ฐ๋ฅ๊ธ์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
let section = NSCollectionLayoutSection(group: group)
let headerFooterSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(44))
let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerFooterSize,
elementKind: ElementKind.sectionHeader,
alignment: .top)
let sectionFooter = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerFooterSize,
elementKind: ElementKind.sectionFooter,
alignment: .bottom)
section.boundarySupplementaryItems = [sectionHeader, sectionFooter]
NSCollectionLayoutDecorationItem
์ปฌ๋ ์ ๋ทฐ์ ์น์ ์ ๋ฐฐ๊ฒฝ์ ์ถ๊ฐํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ ๋๋ค.
⇒ [NSCollectionLayoutSupplementaryItem]
์ ์์ ํด๋์ค
3. NSCollectionViewCompositionalLayout ๊ตฌ์ฑ์์
NSCollectionLayoutEnvironment
๋ ์ด์์์ ์ปจํ ์ด๋ ๋ฐ ํ๊ฒฝ ํน์ฑ(์: ํฌ๊ธฐ ํด๋์ค ๋ฐ ๋์คํ๋ ์ด ๋ฐฐ์จ) ์ ๋ณด ์ ๊ณต์ ์ฌ์ฉํ๋ ํ๋กํ ์ฝ
let layout = UICollectionViewCompositionalLayout { (sectionIndex: Int,
layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection in
if layoutEnvironment.traitCollection.userInterfaceStyle == .dark {
return sectionForUserInterfaceStyle(.dark)
} else {
return sectionForUserInterfaceStyle(.light)
}
}
โ ๊ธฐ๊ธฐ์ ํน์ฑ๊ณผ ๊ด๋ จ๋ ํ๊ฒฝ์ ์๋ ค์ฃผ๋ ํ๋กํ ์ฝ
UICollectionLayoutSectionOrthogonalScrollingBehavior
์ฃผ ๋ ์ด์์ ์ถ๊ณผ ๊ด๋ จ๋ ๋ ์ด์์ ์น์ ์ ์คํฌ๋กค ๋์์ ๋๋ค. enum
๐ก ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ ์น์ ์ ๋ ์ด์์ ๊ตฌ์ฑ์ ์คํฌ๋กค ๋ฐฉํฅ ์์ฑ์ ์ํด ์ ์๋ ๋ ์ด์์์ ์ฃผ์ถ์ ๋ฐ๋ผ ์ฝํ ์ธ ๋ฅผ ๋ฐฐ์นํฉ๋๋ค.
+ ํน์ ์น์ ์ ๋ํ ์ด ๋์์ ๋ณ๊ฒฝํ๋ ค๋ฉด ํด๋น ์น์ ์ ์ง๊ต ์คํฌ๋กค๋ง ๋์ ์์ฑ์ ๊ธฐ๋ณธ๊ฐ์ธ UICollectionLayoutSectionOrthogonalScrollingBehavior.none๊ณผ ๋ค๋ฅธ ๊ฐ์ผ๋ก ์ค์ ํ๋ฉด ๋ฉ๋๋ค.
+ ์ด ์์ฑ์ ๋ค๋ฅธ ๊ฐ์ ์ค์ ํ๋ฉด ์น์ ์ ์ฝํ ์ธ ๊ฐ ์ฃผ ๋ ์ด์์ ์ถ์ ์ง๊ฐ์ผ๋ก ๋ฐฐ์น๋ฉ๋๋ค.
⇒ Enum case
case none
→ ์ฝํ
์ธ ๋ฅผ ์ง๊ฐ์ผ๋ก ์คํฌ๋กคํ์ง ์์
case continuous
→ ์ฐ์ ์คํฌ๋กค์ ํตํด ์ฝํ ์ธ ๋ฅผ ์ง๊ฐ์ผ๋ก ์คํฌ๋กคํ ์ ์์ต๋๋ค.
case continuousGroupLeadingBoundary
→ ์คํฌ๋กคํ์ฌ ํ์๋๋ ๊ทธ๋ฃน์ ์์ชฝ ๊ฒฝ๊ณ์์ ์์ฐ์ค๋ฝ๊ฒ ๋ฉ์ถค
case paging
→ ์ฝํ ์ธ ํ์ด์ง๋ก ์ด๋ + ๋๋ฐ์ด์ค์ ๋๋น, ๋์ด๋ก ์กฐ์ ํ๋ค.
case groupPaging
→ ์ฝํ ์ธ ๋ฅผ ํ ๋ฒ์ ํ ๊ทธ๋ฃน์ฉ ์ง๊ฐ์ผ๋ก ํ์ด์ง์ ํ์ํ ์ ์์ต๋๋ค.
case groupPagingCentered
→ ํ์ด์ง์ ๊ทธ๋ฃน ์ค์ฌ์ ๋ํ๋จ