如何在 swift 4.2 中增加等于屏幕大小的 UICollectionview 单元格大小?

How to increase the UICollectionview Cell Size equal to Screen Size in swift 4.2?

如何在 swift 4.2 中增加 UICollectionView 单元格宽度和高度等于屏幕?

前提是您的 UICollectionViewCell 有一个 UIImageView 的子视图被锚定以完全填充它(即 UIImageView 以顶部、底部、左侧和右侧的方式锚定 UICollectionViewCell)。

可以使用以下方法根据屏幕屏幕屏幕调整 UICollectionViewCell 的大小,这将自动导致 UIImageView 与屏幕本身一样大。

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

        return self.collectionView.bounds.size  //to ensure that this works as intended set self.collectionView size be the screen size.
        }

请注意 UICollectionView 足以容纳您要查找的 UICollectionViewCell 的大小。

您需要更改单元格大小以适合 collectionview。在 viewDidLoad

中添加
if let flow = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flow.itemSize = self.collectionView.frame.size
}