首页 > 解决方案 > 在 collectionview 中以不同大小的网格格式显示图像

问题描述

我想在集合视图中以不同大小的网格格式显示图像,并带有加号(即+2、+3 等)。它看起来像这样...

在此处输入图像描述

我才知道使用UICollectionViewFlowLayout这个可以实现。所以我参考了这个Stack Overflow question 并尝试添加这行代码。但它没有用

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)
        }
    }

此外,如果选择的图像数量是 2 或 3,那么这些图像应该像这样排列..

在此处输入图像描述

我怎样才能使图像看起来像那样......?

标签: iosswiftuicollectionview

解决方案


推荐阅读