2

如何在部分中垂直居中组?

在此处输入图像描述

我在组上尝试了灵活的间距,但看起来并不奏效

group.edgeSpacing = NSCollectionLayoutEdgeSpacing(leading: nil, top: .flexible(10), trailing: nil, bottom: .flexible(10))

完整代码

func buildLayout() {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .absolute(90))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)
    item.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)

    let group1:NSCollectionLayoutGroup
    do {
        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(90))
        group1 = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 2)
        group1.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
        group1.interItemSpacing = .fixed(10)
    }
    let group2:NSCollectionLayoutGroup
    do {
        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(90))
        group2 = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 2)
        group2.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
        group2.interItemSpacing = .fixed(10)
    }

    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
    let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [group1, group2])
    group.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
    group.interItemSpacing = .fixed(10)
    group.edgeSpacing = NSCollectionLayoutEdgeSpacing(leading: nil, top: .flexible(10), trailing: nil, bottom: .flexible(10))

    let section = NSCollectionLayoutSection(group: group)
    section.interGroupSpacing = 10
    section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 20)

    let configuration = UICollectionViewCompositionalLayoutConfiguration()
    configuration.scrollDirection = .horizontal
    collectionViewLayout = UICollectionViewCompositionalLayout(section: section, configuration: configuration)
}
4

0 回答 0