我正在尝试使用 UICollectionViewCompositionalLayout 来创建这个设计:
这里的目的是让集合视图水平滚动,当项目使用 NSCollectionLayoutSection 的orthogonalScrollingBehavior
属性溢出时,它们也会垂直滚动。
这是我现在的布局:
private lazy var collectionViewLayout: UICollectionViewLayout = {
let sectionWidth: CGFloat = 256.0
let layoutItemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(50.0))
let layoutItem = NSCollectionLayoutItem(layoutSize: layoutItemSize)
let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .absolute(sectionWidth), heightDimension: .fractionalHeight(1.0))
let layoutGroup = NSCollectionLayoutGroup.vertical(layoutSize: layoutGroupSize, subitems: [layoutItem])
layoutGroup.interItemSpacing = .fixed(8.0)
let layoutSectionHeaderItemSize = NSCollectionLayoutSize(widthDimension: .absolute(sectionWidth), heightDimension: .absolute(150.0))
let layoutSectionHeaderItem = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: layoutSectionHeaderItemSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
layoutSection.contentInsets = NSDirectionalEdgeInsets(top: 24.0, leading: 24.0, bottom: 24.0, trailing: 24.0)
layoutSection.boundarySupplementaryItems = [layoutSectionHeaderItem]
layoutSection.interGroupSpacing = 8.0
let layoutConfiguration = UICollectionViewCompositionalLayoutConfiguration()
layoutConfiguration.scrollDirection = .horizontal
layoutConfiguration.interSectionSpacing = 16.0
return UICollectionViewCompositionalLayout(section: layoutSection, configuration: layoutConfiguration)
}()
结果是:
关于我哪里出错的任何想法?