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

时间:2018-06-13 11:52:24

标签: ios swift uicollectionview uicollectionviewdelegateflowlayout

我想在我的细胞之间留一个空间,但由于某种原因,它总是为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
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用 minimumInteritemSpacingForSectionAt minimumLineSpacingForSectionAt ,并将返回值设置为您想要的空间:

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
}