简体   繁体   English

在水平滚动中顶部具有不同高度的集合视图Pin单元格

[英]Collection View Pin Cell With Different hight to top in a horizontal scrolling

I have a horizontal collection view with different cell heights. 我有一个具有不同像元高度的水平收集视图。 By default the cells are centered in the middle of the collection view. 默认情况下,单元格位于集合视图的中间。

What I need is pin them to the top of the collection view not in the center so the extra space will be beneath the cells. 我需要的是将它们固定在集合视图的顶部,而不是在中心,这样多余的空间将位于单元格的下方。

I have only one row with a total of 4 cells. 我只有一行,共有4个单元格。

在此处输入图片说明

I have found the solution. 我找到了解决方案。 Simply add this code in cellForItem method . 只需在cellForItem方法中添加此代码。

cell.frame = CGRect(origin: CGPoint(x: cell.frame.minX, y: 0), size: cell.frame.size)

Also

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    collectionView.reloadData()
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM