0
如何使用集合视图实现图像中显示的布局?如何在集合视图中实现不同行数的列数
我试图使用瀑布流布局从下面的链接,但没有获得成功,去实现它。 https://github.com/chiahsien/CHTCollectionViewWaterfallLayout
下面是我尝试这样做的代码。
override func viewDidLoad() {
super.viewDidLoad()
let identifier = String(describing: collectionCell.self)
collectionView.register("collectionCell", forCellWithReuseIdentifier: "collectionCell")
collectionView.register(UINib(nibName: "collectionCell", bundle: nil), forCellWithReuseIdentifier: identifier)
let layout = CHTCollectionViewWaterfallLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
layout.minimumInteritemSpacing = 0
layout.minimumColumnSpacing = 10
collectionView.collectionViewLayout = layout
}
//用于设置电池
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let width = Double((self.collectionView.bounds.size.width-(4*minimumSpaceBetweenCells))/3)
if indexPath.row == 0 {
return CGSize(width:width * 2, height: width * 2)
}
else
{
return CGSize(width:width, height: width)
}
}
的大小,但它只是显示2列大小相同的所有行。
请让我知道,如果有人有想法如何做到这一点。
在此先感谢。
我认为你必须编码布局。请参阅在互联网上找到的大多数教程,它们分别具有相同的项目高度或宽度以及可变的宽度或高度。在你的情况下,你有两种不同高度和宽度的物品。你必须自己布置这些物品。 –
http://stackoverflow.com/questions/43186246/uicollectionview-layout-like-snapchat/43409440#43409440?只需计算自己的框架,这很容易。 – Larme
谢谢@Larme我从你提供的链接创建了一个快速版本,它适用于我。 –