1

我正在使用UICollectionViewDelegateFlowLayout委托使其成为方形,但最后显示出奇怪的间距。

在此处输入图像描述 在此处输入图像描述

我的代码

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

    if collectionView == self.bannerClcView {

        let cellSize = CGSize.init(width: view.frame.size.width, height: view.frame.size.height)
        return cellSize

    }else if collectionView == self.category {

        let cellSize = CGSize.init(width: (self.category.frame.size.width/3), height: category.frame.size.width/3)
        return cellSize

    }else {

        let cellSize = CGSize.init(width: (recomended_Ads_Clc.frame.size.width/2), height: 220)
        return cellSize

    }
}


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

2 回答 2

0

如果你打开你的 StoryboardUICollectionView并选择它的尾随约束,我想你会在属性检查器中看到类似这样的东西:

在此处输入图像描述

您需要取消选中“相对于边距”以删除您看到的额外空间。

于 2019-04-12T13:26:12.800 回答
0

您的错误在于您计算函数内单元格的宽度属性的源视图

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

代替

let cellSize = CGSize.init(width: (recomended_Ads_Clc.frame.size.width/2), height: 220) return cellSize

return CGSize(width: (collectionView.frame.width / 2) - 10, height: 220)

并根据需要计算高度。10 将是单元格之间的空间

于 2019-04-17T13:50:27.207 回答