【问题标题】:UICollectionViewCell not filling screen widthUICollectionViewCell 不填充屏幕宽度
【发布时间】:2019-02-24 22:13:41
【问题描述】:

我试图让我的UICollectionView 的单元格填满整个宽度,但它看起来像这样:

我尝试过这样的事情:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height+140)
    }

但它什么也没做。提示?

【问题讨论】:

    标签: ios swift xcode uicollectionview


    【解决方案1】:

    您需要使用sizeForItemAt 而不是sizeForItemAtIndexPath。 sizeForItemAtIndexPath 向代理询问指定项目单元格的大小。

    你需要使用 Swift 3+:

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height+140)
    }
    

    【讨论】: