아이템간 간격을 주지 않은 경우
아이템간 간격을 준 경우
private func setCollectionLayouts(){
let layout = UICollectionViewFlowLayout()
let spacing:CGFloat = 20; let groupItemsCount = 2;
let cellWidth: Int = (Int(UIScreen.main.bounds.width) -
Int(spacing) * (groupItemsCount + 1)) / groupItemsCount
guard cellWidth > 0 else { fatalError("이건 아니지") }
layout.itemSize = .init(width: cellWidth,height: cellWidth)
layout.sectionInset = .init(top: spacing, left: spacing,
bottom: spacing, right: spacing)
// 그룹간의 간격
layout.minimumLineSpacing = spacing
// 아이템간의 간격
layout.minimumInteritemSpacing = spacing
collectionView.collectionViewLayout = layout
}
💡 이 코드에서 뷰의 너비를 300정도로 잡으면 cellWidth는 대략 11~12pt이다.
이 그룹의 아이템 계수는 9개로 11 * 9 = 99pt 정도의 너비만 사용한다.
결국 모든 아이템을 한 줄에 채우고 자동으로 채우는 너비의 간격이 정해지지 않아서 우측에 남는 공간이 발생하는 것을 볼 수 있다.
'이모저모 > UIKit' 카테고리의 다른 글
Modern UIKit Collection, TableView #2-1 (0) | 2023.09.03 |
---|---|
Modern UIKit Collection, TableView #1 (0) | 2023.08.25 |
WWDC) Customize and resize sheets in UIKit (0) | 2023.08.02 |
테이블 셀 내부 UIButton에 addTarget을 계속 해도 되는이유 (0) | 2023.08.01 |
In-line Label 처리하기 (0) | 2023.07.26 |
댓글