更改使用XIB文件创建的垂直像元大小空间

时间:2019-11-12 00:52:25

标签: swift xcode

如何更改单元格之间的空间?当前看起来像这样:

enter image description here

我尝试过的所有方法都无效,这是我当前的设置:

enter image description here

这是我定义单元格的地方:

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return products.count
        return 3
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // This determines the size of the cell on the featured slider
        return CGSize(width: UIScreen.main.bounds.width, height: 350)
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedPostsCollectionViewCell", for: indexPath) as! FeaturedPostsCollectionViewCell
        let product = products[indexPath.row]
        cell.featuredImage.image = UIImage(named: "4")
        cell.lbName.text = product.Name!
        cell.lbDesc.text = product.desc!

        return cell
    }

我环顾四周,却找不到给它这么多空间的东西...

1 个答案:

答案 0 :(得分:0)

我刚刚找到解决方案,碰巧我不得不将宽度固定为我的首选值:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 310, height: 350)
}