如何删除集合视图单元格之间的空间?

时间:2018-06-11 11:16:08

标签: ios swift uicollectionview flowlayout

我有3个单元格,第一个单元格有标签,第二个单元格有 textfield ,最后一个单元格有按钮,使用它给出自动大小,但如何删除单元格之间的空间

if let flowlayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowlayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
        }

或如何在我的场景中使用此方法

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    code
}

enter image description here

6 个答案:

答案 0 :(得分:1)

请参阅:https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout

let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.minimumInteritemSpacing = 0 // The minimum spacing to use between items in the same row.
myCollectionView.layout = layout // or myCollectionView.collectionViewLayout ( I don't have compiler here so I do not know the property name, but I know it has property that has layout in its name )

答案 1 :(得分:1)

使用此:

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

与两侧的间距:

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

答案 2 :(得分:1)

如果您的控制器有一个 init 函数,您可以使用 minimumLineSpacingminimumInteritemSpacing 方法避免将其批量化,并执行以下操作:

init() {
    
    let layout = UICollectionViewFlowLayout()
    layout.minimumLineSpacing = 0
    layout.minimumInteritemSpacing = 0
    
    super.init(collectionViewLayout: layout)
}

答案 3 :(得分:0)

Swift 4.0

UICollectionViewDelegateFlowLayout有2个委托方法可以删除项目和部分之间的空格。

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

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

答案 4 :(得分:0)

该单元格位于中心,我使用此库AlignedCollectionViewFlowLayout将单元格向左对齐 ,现在空间工作了,我给了多少空间

答案 5 :(得分:0)

快速5

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

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