Post not yet marked as solved
Post marked as unsolved with 0 replies, 1,313 views
Hi All,I'm working to move all of my CollectionView grids over to Compositional Layouts. It's been smooth sailing until I have to create a cell in a certain aspect ratio (1.0 width, 1.33 height) - so it's essentially a rectangle, but the height is a ratio (1.33) of the cells width - no matter how many columns I use.I've got 4 columns across in the example code below - and was able to get it to paint alright - but it's not based on any aspect ratio of the cell - but rather the aspect at the whole container width. So if I change the count to 5 columns, the cells will be way too high.Any ideas? I'm sure it's something simple, but I'm just overthinking this.Thanks!func createLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalWidth(0.3))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 4)
let spacing = CGFloat(25)
group.interItemSpacing = .fixed(spacing)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = spacing
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 25, bottom: 0, trailing: 25)
let headerFooterSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(100))
let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: headerFooterSize,
elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
section.boundarySupplementaryItems = [sectionHeader]
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}