Note that Apple now have UICollectionViewCompositionalLayout, which is trivial to use.
(Essentially just set fractionalWidth to 1.)
All previous apple systems for sizing collection views were garbage, and they finally fixed it with UICollectionViewCompositionalLayout.
In a vertical UICollectionView
,
Is it possible to have full-width cells, but, allow the dynamic height to be controlled by autolayout?
This strikes me as perhaps the "most important question in iOS with no really good answer."
When using UICollectionViewCompositionalLayout
make sure you set both itemSize
and groupSize
height to .estimated
:
My problem was that I was using .fractional(1)
for item height
and .estimated(44)
for group
and it didn't work.
extension TasksController {
static func createLayout() -> UICollectionViewLayout {
UICollectionViewCompositionalLayout { (sectionIndex: Int, _ NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let height: CGFloat = 44
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(height))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(height))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: columns)
let section = NSCollectionLayoutSection(group: group)
return section
}
}
}