티스토리 뷰

반응형

CollectionViewCompositionalLayout은 간격을 조정하는 여러 가지 방법이 있습니다. item 간의 간격, section 간의 간격, header 및 footer의 크기와 간격 등을 조정할 수 있습니다.

item 간의 간격을 조정하려면 NSCollectionLayoutSpacing 객체를 사용할 수 있습니다. 이 객체는 space, lineSpacing, and interItemSpacing 같은 여러가지 속성을 가지고 있으며, 이를 조정함으로써 item 간의 간격을 조정할 수 있습니다.

예를 들어, 아래 코드는 NSCollectionLayoutSpacing을 사용하여 각 item 간의 간격을 10으로 설정하는 방법을 보여줍니다.

 

Swift 코드

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

let itemSpacing = CGFloat(10)
let groupSpacing = CGFloat(10)

let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(50))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = groupSpacing
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
section.interGroupSpacing = groupSpacing
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)

let spacing = NSCollectionLayoutSpacing.fixed(itemSpacing)
section.contentInsets.bottom = itemSpacing // 종료 item의 마진 주기
section.interItemSpacing = spacing

let layout = UICollectionViewCompositionalLayout(section: section)


따라서, item의 contentInset을 조정하지 않고도 item 간의 간격을 조정할 수 있습니다.

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함