如何使用swift 4显示像facebook上传图像的collectionView?

Gor*_*per 2 ios uicollectionview swift swift4 uicollectionviewflowlayout

在此输入图像描述在此输入图像描述

您好我想将collectionView完全显示为上图.我知道它负责UICollectionViewFlowLayout,但无法做到.非常感谢帮助.这是我的代码

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
     let yourWidth = (collectionView.bounds.width - 4) / 3.0
     let yourHeight = yourWidth
     return CGSize(width: yourWidth, height: yourHeight)
}
Run Code Online (Sandbox Code Playgroud)

Jee*_*ari 5

您的答案在您的问题中缺乏条件

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        switch indexPath.item {
        case 0,1:
            return CGSize(width: (UIScreen.main.bounds.width - 16) / 2, height: (UIScreen.main.bounds.width - 16) / 2)
        default:
            return CGSize(width: (UIScreen.main.bounds.width - 32) / 3, height:  (UIScreen.main.bounds.width) / 3)
        }
    }
Run Code Online (Sandbox Code Playgroud)