UICollectionView Set number of columns

后端 未结 17 1206
执笔经年
执笔经年 2020-11-28 17:28

I just started learning about UICollectionViews. I\'m wondering if anyone knows how to specify the number of columns in a collectionview. The default is set to 3 (iPhone/por

17条回答
  •  一个人的身影
    2020-11-28 18:29

    First add UICollectionViewDelegateFlowLayout as protocol.

    Then:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
            var columnCount = 3
            let width  = (view.frame.width - 20) / columnCount
            return CGSize(width: width, height: width)
    }
    

提交回复
热议问题