UICollectionViewCell根据图像的高度,处理单元格之间的间距

时间:2018-07-26 14:04:04

标签: ios swift uicollectionview uicollectionviewcell

我有一个UICollectionView由图像组成,分为两列。

现在,根据图像aspect ratio计算像元高度。因此,每个单元格的高度都不同。

所以,我得到以下结果。

如何管理图像顶部和底部的空间。

请参见附加图像中的带注释的矩形。

Screenshot

编辑: 参见UICollectionViewCell中具有 red backgroundColor 的更新的屏幕截图。

enter image description here

编辑: 我使用CHTCollectionViewWaterfallLayout/Swift库实现了输出。请参见下面的屏幕截图。

enter image description here

谢谢。

1 个答案:

答案 0 :(得分:1)

我不认为这种布局可以使用普通的UICollectionViewFlowLayout来完成,我认为您应该考虑创建自己的自定义UICollectionViewLayout来实现这一目标。

这基本上是Pintrest风格的UICollectionViewLayout,因此您可以按照以下教程进行操作:

UICollectionView Custom Layout Tutorial: Pinterest

或者只是从github projet获取它:

PinterestLayout

CHTCollectionViewWaterfallLayout