[Swift] horizontal(layoutSize:subitem:count:)
와 horizontal(layoutSize:repeatingSubitem:count:)
들어가기 전에
NSCollectionLayoutGroup
의 타입 메서드인 horizontal(layoutSize:subitem:count:)
가 deperecated 되었습니다.
🤔 : 엥? 난 저게 뭔지 애초에 모르는데;;; 저거 먼저 설명해야하는거 아니냐?;;
ㅎㅎ.. 그럼 이 함수가 뭔지, deprecated 되었으니 대신 사용해야하는 함수는 어떻게 동작하는지..! 시작해봅시다.
horizontal(layoutSize:subitem:count:)
NSCollectionLayoutSection
을 생성할때 이런식으로 group 을 만들어서 넘기면, 동일한 크기의 subitem 을 count
만큼 포함하는 그룹을 만듭니다.
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,
subitem: item,
count: 1)
보다시피 그룹 자체의 width
는 전체 화면의 크기만큼 설정했습니다.
여기서 count
를 2, 3, 4 로 각각 변경해볼까요?
item 의 사이즈가 조절 되면서 count
만큼 수평 영역이 채워지네요! count
의 역할이 꽤나 직관적이죠?
제가 아래 코드와 같이 itemSize
의 width
를 100 으로 지정했대도, 인자로 넘긴 count
에 따라서 subitem
들의 width
가 자동으로 계산되기 때문에 subitem
에서 설정한 width 는 무시됩니다.
let sectionProvider = { (sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemWidth: CGFloat = 100
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(itemWidth),
heightDimension: .estimated(30))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,
subitem: item,
count: 2)
let section: NSCollectionLayoutSection = NSCollectionLayoutSection(group: group)
return section
}
하지만 이러한 horizontal(layoutSize:subitem:count:)
가 Deprecated 되었습니다.
horizontal(layoutSize:repeatingSubitem:count:)
두둥. deprecated 된 위 함수를 대신해서 대신 horizontal(layoutSize:repeatingSubitem:count:)
함수를 사용해야합니다.
근데? item 반복 횟수에 맞게 group
의 layoutSize
를 맞추는건 개발자의 역할입니다.
🤔 : 엥?? width 를 내가 직접 맞춰야하면 인자로 받는 count
는 도대체 역할이 뭔데?;;;
아까처럼 item 의 width
를 100 으로 두고, 요 함수의 count 를 1 로 설정해볼까요?
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,
repeatingSubitem: item,
count: 1)
보다시피 width
100 을 유지한채로, 한줄에 설정한 count
만큼 item 이 노출됩니다.
그러면 count
를 2, 3, 4 으로 각각 변경해보았습니다.
마찬가지로 item 에 설정한 width
를 유지한채로, 한줄에 설정한 count
만큼 노출됩니다. 만약 기기의 사이즈가 충분하지 않는다면 그냥 잘리는군요.
count
가 각각 3, 4일때 두번째 라인의 첫번째 item 이 다른걸 볼 수 있는데요 (Apple Store vs 팟캐스트), 이를 보아 노출되지 않은 아이템이 자동으로 다음 line 으로 내려온다거나.. 그런 일은 없다는걸 확인할 수 있습니다.
마무리
이제 각각의 정의를 다시 확인해볼까요?
horizontal(layoutSize:subitem:count:)
지정된 크기의 그룹을 만듭니다. 여기에는 count 로 지정된 수만큼 수평으로 정렬된 동일한 크기의 항목 배열이 포함됩니다.
Creates a group of the specified size, containing an array of equally sized items arranged in a horizontal line up to the number specified by count.
horizontal(layoutSize:repeatingSubitem:count:)
수평 축을 따라 지정된 항목을 특정 횟수로 반복하는 그룹을 만듭니다.
Creates a group that repeats the specified subitem a certain number of times along the horizontal axis.
horizontal(layoutSize:repeatingSubitem:count:)
에는 "동일한 크기" 얘기가 쏙 빠졌군요!
그럼 이 차이점을 이해하고...잘 사용하십셔..! (물론 제 얘기) 그럼 20000!