我们如何在UICollectionView的底部和顶部添加边距?

时间:2018-10-01 09:31:10

标签: ios swift uicollectionview

我是新手,这是一种使CollectionView在屏幕中具有4个单元格的解决方案,在下面的行中,我说每侧15边距,这对左右均有效,但对顶部和按钮不起作用

return UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)

,我们怎么在顶部和底部有相同的边距?

这里是 MainViewController

import UIKit

class MainViewController: UIViewController , UICollectionViewDelegate, UICollectionViewDataSource {

    var listArray : [String] = []
    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("MainViewController")
        collectionView.delegate = self
        collectionView.dataSource = self

        for index in 1 ..< 12 {
            listArray.append("Cell \(index)")
        }
    }


    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return listArray.count
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mainCell", for: indexPath) as! MainCollectionViewCell
        let cellIndex = indexPath.item
        cell.cellLabelText.text = listArray[cellIndex]
        cell.backgroundColor = UIColor.blue

        return cell
    }


}


extension MainViewController : UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let bounds = collectionView.bounds

        return CGSize(width: (bounds.width / 2 ) - 30   , height:  ( bounds.height / 2 ) - 30   )
    }

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

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

}

这里是 MainCollectionViewCell

import UIKit

class MainCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var cellLabelText: UILabel!

}

enter image description here

4 个答案:

答案 0 :(得分:3)

由于您的收藏夹视图只有1个部分,因此您需要正确调整一个部分中不同行/列与同一行/列中不同项目之间的间距,而不是设置这些部分的插图。

UICollectionViewDelegateFlowLayout具有以下两种实现目标的方法:collectionView(_:layout:minimumLineSpacingForSectionAt:)collectionView(_:layout:minimumInteritemSpacingForSectionAt:)

要复制您当前用于各节的间距,您需要实现以下方法:

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

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

答案 1 :(得分:0)

选择故事板:

1)选择UICollection视图

2)设置Section Insets for TOP and BOTTOM

enter image description here

我希望这会有所帮助!

答案 2 :(得分:0)

尝试

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForItemAt indexPath: IndexPath) -> UIEdgeInsets {

                return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
            }

答案 3 :(得分:0)

您应像这样设置 UICollectionViewFlowLayout 的“ sectionInset ”属性

if let flowLayout = yourCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        flowLayout.sectionInset = UIEdgeInsetsMake(100, 0, 100, 0)
    }

这将在集合视图中将上边距和下边距增加100点。顺便说一句,该序列在 UIEdgeInsetsMake 方法中位于顶部,左侧,底部,右侧。