KWiOS
KWiOS0101
KWiOS
  • 분류 전체보기 (108)
    • Algorithm (41)
      • 이코테 (14)
      • 이코테 문제풀이 (21)
      • 프로그래머스 (6)
    • CS (1)
      • 모두를 위한 컴퓨터 과학(CS50 2019) (0)
    • iOS (15)
    • Swift (36)
      • Swift문법 (32)
      • 기타 (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 6

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
KWiOS

KWiOS0101

iOS

컴포지션 레이아웃에서 데이터에따라 섹션 분기처리

2024. 3. 10. 20:00

리뷰 데이터가 있을때와 없을때 UI를 따로 처리해야하는 경우에 아래 코드처럼 분기 처리

func createReviewColumnSection() -> NSCollectionLayoutSection {
        
        if viewModel?.isReviews == false {
            // Item
            let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),heightDimension: .estimated(1))
            let item = NSCollectionLayoutItem(layoutSize: itemSize)
            
            let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),heightDimension: .estimated(1))
            let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
            
            // Section
            let section = NSCollectionLayoutSection(group: group)
            section.contentInsets = NSDirectionalEdgeInsets(top: 16, leading: 20, bottom: 24, trailing: 20)
            section.interGroupSpacing = 16
            
            // Header
            section.boundarySupplementaryItems = [
                NSCollectionLayoutBoundarySupplementaryItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(41)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
            ]
            
            // Background
            let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(elementKind: "GrayBackgroundViewInset")
            section.decorationItems = [sectionBackgroundDecoration]
            
            return section
        } else {
            // Item
            let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),heightDimension: .estimated(1))
            let item = NSCollectionLayoutItem(layoutSize: itemSize)
            
            let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),heightDimension: .estimated(1))
            let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
            
            // Section
            let section = NSCollectionLayoutSection(group: group)
            section.contentInsets = NSDirectionalEdgeInsets(top: 16, leading: 20, bottom: 24, trailing: 20)
            section.interGroupSpacing = 16
            
            // Header
            section.boundarySupplementaryItems = [
                NSCollectionLayoutBoundarySupplementaryItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(41)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .top),
                NSCollectionLayoutBoundarySupplementaryItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(61)), elementKind: UICollectionView.elementKindSectionFooter, alignment: .bottom)
            ]
            
            // Background
            let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(elementKind: "GrayBackgroundViewInset")
            section.decorationItems = [sectionBackgroundDecoration]
            
            return section
        }
    }

 

'iOS' 카테고리의 다른 글

iOS - CollectionView Cell 뱃지 만들기  (1) 2024.03.19
iOS - 사파리 웹 인스펙터로 WebView 디버깅 하기  (0) 2024.03.17
collectionView - 가로방향으로 셀 3개 넣기  (0) 2024.01.28
iOS - 공유하기 기능  (0) 2023.08.06
iOS - StackView에서 Subview 제거할때 주의점  (0) 2023.07.24
    'iOS' 카테고리의 다른 글
    • iOS - CollectionView Cell 뱃지 만들기
    • iOS - 사파리 웹 인스펙터로 WebView 디버깅 하기
    • collectionView - 가로방향으로 셀 3개 넣기
    • iOS - 공유하기 기능
    KWiOS
    KWiOS

    티스토리툴바