TIL119 ✨

YaR LabΒ·2023λ…„ 10μ›” 10일
0

TIL✨

λͺ©λ‘ 보기
98/136
post-thumbnail

πŸ—“οΈ23.10.05

Sticky Header (with compositionalLayout)

  • UICollectionLayoutListConfiguration κ³Ό ν•¨κ»˜ 헀더λ₯Ό κ³ μ •ν•˜λŠ” 법을 μ°Ύλ‹€ 도무지 찾을 방법이 μ—†μ–΄ μ›μ΄ˆμ μΈ λ°©λ²•μœΌλ‘œ pinToVisibleBounds을 섀정해쀬닀.
    private func listLayout() -> UICollectionViewCompositionalLayout {
        let itemSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1.0),
            heightDimension: .estimated(80))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        
        let groupSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1.0),
            heightDimension: .estimated(80))
        let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
        group.edgeSpacing = NSCollectionLayoutEdgeSpacing(leading: nil, top: .fixed(3), trailing: nil, bottom: nil)
        let section = NSCollectionLayoutSection(group: group)
        section.contentInsets = NSDirectionalEdgeInsets(top: 3, leading: 0, bottom: 0, trailing: 0)
        
        let headerSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1.0),
            heightDimension: .estimated(80))
        let headerSupplementary = NSCollectionLayoutBoundarySupplementaryItem(
            layoutSize: headerSize,
            elementKind: ElementKind.sectionHeader,
            alignment: .top)
        headerSupplementary.pinToVisibleBounds = true
        section.boundarySupplementaryItems = [headerSupplementary]
        
        return UICollectionViewCompositionalLayout(section: section)
    }
  • μ°Ύκ³  찾은 끝에 UICollectionLayoutListConfigurationκ³Ό ν•¨κ»˜ 헀더λ₯Ό μ„€μ •ν•˜λŠ” 방법을 μ°Ύμ•˜κ³ , ν—€λ”λŠ” κ³ μ • λμ§€λ§Œ κ·Έ μœ„λ‘œ 셀이 μ˜¬λΌκ°€λŠ” λ¬Έμ œκ°€ λ°œμƒν–ˆλ‹€. zIndexλ₯Ό μ„€μ •ν•΄μ€˜μ„œ 헀더λ₯Ό μœ„λ‘œ λŒμ–΄μ˜¬λ¦¬λŠ” 방법을 μ°Ύμ•˜λ‹€!!
    private func listLayout() -> UICollectionViewCompositionalLayout {
        let layout = UICollectionViewCompositionalLayout { _, layoutEnvironment in
            
            var configuration = UICollectionLayoutListConfiguration(appearance: .grouped)
            configuration.headerMode = .supplementary
            configuration.backgroundColor = .systemGray6
            
            let section = NSCollectionLayoutSection.list(using: configuration, layoutEnvironment: layoutEnvironment)
            section.contentInsets = NSDirectionalEdgeInsets(top: 7, leading: 0, bottom: 0, trailing: 0)
            section.interGroupSpacing = 7
            let headerSize = NSCollectionLayoutSize(
                widthDimension: .fractionalWidth(1.0),
                heightDimension: .estimated(80))
            let headerSupplementary = NSCollectionLayoutBoundarySupplementaryItem(
                layoutSize: headerSize,
                elementKind: ElementKind.sectionHeader,
                alignment: .top)
            
            headerSupplementary.pinToVisibleBounds = true
            headerSupplementary.zIndex = 110
            section.boundarySupplementaryItems = [headerSupplementary]
            return section
        }
        
        return layout
    }
  1. CompositionalLayoutμ—μ„œ Cell 및 Supplementary View(header λ˜λŠ” footer λ“±)의 μ‚¬μ΄μ¦ˆλ₯Ό μž‘μ•„μ€Œ
  2. DiffableDataSourceλ₯Ό 톡해 Custom View 및 Supplementary Viewλ₯Ό Collection View에 λ„˜κ²¨μ€Œ
  • κΈ°λ³Έ list configuration을 썼을 λ•Œ headerMode만 μ„€μ •ν•΄μ€˜λ„ ν•΄λ”μ˜ λ ˆμ΄μ•„μ›ƒμ΄ μž‘νžˆλŠ” 이유
    • configurationμ—μ„œ 기본적으둜 헀더 μ‚¬μ΄μ¦ˆλ₯Ό μž‘μ•„μ£ΌκΈ° λ•Œλ¬Έ
    • λ”°λΌμ„œ 헀더에 λŒ€ν•œ λ ˆμ΄μ•„μ›ƒμ„ μ»€μŠ€ν„°λ§ˆμ΄μ§•μ„ ν•˜κΈ° μœ„ν•΄μ„œλŠ” configuration으둜 section을 λ§Œλ“€κ³  μ»€μŠ€ν„°λ§ˆμ΄μ§•ν•œ 헀더λ₯Ό λ„£μ–΄μ£Όλ©΄ 됨

πŸ“š 참고링크

https://nemecek.be/blog/83/how-to-create-sticky-headers-with-compositional-layout
https://techblog.gccompany.co.kr/compositional-layoutκ³Ό-diffable-datasource둜-ν™ˆ-λ¦¬νŒ©ν† λ§ν•˜κΈ°-d80ac0d11edd
https://velog.io/@hyesuuou/iOSSwift-UICollectionLayoutListConfiguration.plain에-헀더λ₯Ό-λ„£μ—ˆμ„-λ•Œ-ν—€λ”μœ„μ—-λ­”κ°€-여백이-μƒκΈ°λŠ”-경우
https://medium.com/@pruthvi187/uicollectionview-list-layout-beb22f5a7021
https://developer.apple.com/documentation/uikit/uicollectionviewcompositionallayoutsectionprovider
https://developer.apple.com/documentation/uikit/uicollectionlayoutlistconfiguration
https://developer.apple.com/documentation/uikit/views_and_controls/collection_views/implementing_modern_collection_views#4208526

0개의 λŒ“κΈ€