简体   繁体   English

如何快速制作可水平调整大小的单元格的collectionView

[英]How to make a collectionView with resizable cells horizontally in swift

I want to create a collection view that has different sized cells. 我想创建一个具有不同大小的单元格的集合视图。 My final outcome is something like this picture 我的最终结果是这样的照片

图片 .

Your class needs to also implement the UICollectionViewDelegateFlowLayout protocol. 您的类还需要实现UICollectionViewDelegateFlowLayout协议。 Initialize the collection view like so: 初始化集合视图,如下所示:

let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.delegate = self

And then you can implement the function sizeForItemAt : 然后可以实现功能sizeForItemAt

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = // get the width
        let height = // get the height
        return CGSize(width: width, height: height)
    }

As long as your screen can fit multiple of your widths, the collection view will automatically fit them horizontally. 只要您的屏幕可以容纳多个宽度,集合视图就会自动将它们水平放置。

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

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