【iOS】UICollectionView 详解

一、UICollectionView水平、竖直间隙问题,可通过itemSize、contentInset、minimumLineSpacing、minimumInteritemSpacing调节

// CollectionView
lazy var imageCollectionView: UICollectionView = {
        // 左、中、右等间隙15
        let kItemWidth = (UIScreen.main.bounds.width - 15*3) / 2.0
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: kItemWidth, height: kItemWidth*0.75)
        flowLayout.scrollDirection = .vertical
        // 行间隙15
        flowLayout.minimumLineSpacing = 15
        flowLayout.minimumInteritemSpacing = 0
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.backgroundColor = .clear
        
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(WSFServiceCompleteImageCollectionCell.self, forCellWithReuseIdentifier: NSStringFromClass(WSFServiceCompleteImageCollectionCell.self))
         // 上、左、右间隙15
        collectionView.contentInset = UIEdgeInsets(top: 15, left: 15, bottom: 0, right: 15)
        
        return collectionView
    }()

override func addConstraint() {
        super.addConstraint()
        imageCollectionView.snp.makeConstraints { make in
            make.top.leading.trailing.bottom.equalTo(self.view)
        }
}

// 单元格
private func addConstraints() {
        containerView.snp.makeConstraints { make in
            // 顶部间隙15
            make.edges.equalTo(self.contentView).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
}

image.png

你可能感兴趣的:(【iOS】UICollectionView 详解)