UICollectionView, simply fit cell to width?

前端 未结 3 1732
予麋鹿
予麋鹿 2020-12-14 06:20

Here\'s a simple UICollectionView in yellow

\"enter

The red arrow sets the wid

3条回答
  •  鱼传尺愫
    2020-12-14 06:58

    There is a simple way to do this. Here is the swift code. Assumes you want one item wide and in the example my cells are 64 pts high, and I want 8 pts for side margins, and finally collectionView is the name of your UICollectionView.

    Insert this into viewDidLoad :

        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize.init(width: (UIScreen.main.bounds.width - 16), height: 64.0)
    

    This will set all the cells to default to that size. You can still override individual cells if you subclass UICollectionViewLayout or UICollectionViewFlowLayout, as mentioned in other answers. But if you simply want to have more table view like behavior this should work.

提交回复
热议问题