Space 集合视图单元格之间始终为 0

Space between collection view cells is always 0

我试图在我的单元格之间设置一个 space,但由于某种原因它始终为 0。我做错了什么?

import UIKit

class AlbumPlayerProgressBar: UICollectionView {

    ...

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.register(UINib(nibName: "ProgressBarCell", bundle: nil), forCellWithReuseIdentifier: NSStringFromClass(ProgressBarCell.self))
        self.dataSource = self
        self.delegate = self
    }
}

extension AlbumPlayerProgressBar: UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return numOfSlides
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(ProgressBarCell.self), for: indexPath) as! ProgressBarCell

        ...
        return cell
    }
}

extension AlbumPlayerProgressBar: UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize
    {       
        return CGSize(width: segmentWidth, height: segmentHeight)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
    {
        return 100
    }
}

您可以使用 minimumInteritemSpacingForSectionAtminimumLineSpacingForSectionAt ,并将 return 值设置为您想要的 space :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}