Skip to content

Commit

Permalink
Refactor: HomeViewController에 있던 Compositional layout Section 생성 코드 확장
Browse files Browse the repository at this point in the history
- 유틸성 있게 만들기 위해서는 아이템 개수, 아이템 높이 및 너비까지 파라미터로 받아서 처리해야 할 것
  • Loading branch information
Minny27 committed Jan 23, 2024
1 parent fbcb0df commit 0bfe20d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//
// UICollectionViewCompositionalLayout+.swift
// Nagaza
//
// Created by SeungMin on 1/23/24.
//

import UIKit

struct ElementKind {
static let badge = "badge-element-kind"
static let background = "background-element-kind"
static let sectionHeader = "section-header-element-kind"
static let sectionFooter = "section-footer-element-kind"
static let layoutHeader = "layout-header-element-kind"
static let layoutFooter = "layout-footer-element-kind"
}

extension UICollectionViewCompositionalLayout {
static func listLayout(withEstimatedHeight estimatedHeight: CGFloat = 100) -> UICollectionViewCompositionalLayout {

return UICollectionViewCompositionalLayout(section: .listSection(withEstimatedHeight: estimatedHeight))
}

static func listLayoutAutomaticHeight(withEstimatedHeight estimatedHeight: CGFloat = 100) -> UICollectionViewCompositionalLayout {

return UICollectionViewCompositionalLayout(section:
.listEstimatedHeightSection(withEstimatedHeight: estimatedHeight))
}
}

extension NSCollectionLayoutSection {
static func listSection(withEstimatedHeight estimatedHeight: CGFloat = 100) -> NSCollectionLayoutSection {
let itemWidth: CGFloat = 100
let itemCount = 10

let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0)
)
let item = NSCollectionLayoutItem(layoutSize: itemSize)

let groupSize = NSCollectionLayoutSize(
widthDimension: .absolute(itemWidth * CGFloat(itemCount)),
heightDimension: .estimated(estimatedHeight)
)

let group = NSCollectionLayoutGroup.horizontal(
layoutSize: groupSize,
subitem: item,
count: itemCount
)
group.interItemSpacing = .fixed(16)

let sectionHeaderSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(45))

let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: sectionHeaderSize,
elementKind: ElementKind.sectionHeader,
alignment: .top
)

let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(
top: 0,
leading: 20,
bottom: 0,
trailing: 20
)
// section.interGroupSpacing = 20 // 적용이 안 됨
section.boundarySupplementaryItems = [sectionHeader]
section.orthogonalScrollingBehavior = .continuousGroupLeadingBoundary

return section
}

static func listEstimatedHeightSection(withEstimatedHeight estimatedHeight: CGFloat = 100) -> NSCollectionLayoutSection {
let itemWidth: CGFloat = 100
let itemCount = 10

let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(estimatedHeight)
)
let item = NSCollectionLayoutItem(layoutSize: itemSize)

let groupSize = NSCollectionLayoutSize(
widthDimension: .absolute(itemWidth * CGFloat(itemCount)),
heightDimension: .estimated(estimatedHeight)
)

let group = NSCollectionLayoutGroup.horizontal(
layoutSize: groupSize,
subitem: item,
count: itemCount
)
group.interItemSpacing = .fixed(16)

let sectionHeaderSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(45))

let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: sectionHeaderSize,
elementKind: ElementKind.sectionHeader,
alignment: .top
)

let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(
top: 0,
leading: 20,
bottom: 0,
trailing: 20
)
// section.interGroupSpacing = 20 // 적용이 안 됨
section.boundarySupplementaryItems = [sectionHeader]
section.orthogonalScrollingBehavior = .continuousGroupLeadingBoundary

return section
}
}

Binary file added graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0bfe20d

Please sign in to comment.