简体   繁体   English

如何更改集合视图单元格大小并添加填充?

[英]How to change collection view cell size and add padding?

Im trying to get my collectionView cells to look like the picture bellow but have not been able to do it. 我试图让我的collectionView单元看起来像下面的图片,但一直无法做到。 I tried using collectionViewLayout but was not able to get the sizing right along with the proper padding. 我尝试使用collectionViewLayout但是无法正确调整大小以及正确的填充。 How would I be able to do this? 我怎么能这样做?

Here is my code 这是我的代码

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let frameSize = collectionView.frame.size
            return CGSize(width: frameSize.width, height: frameSize.height / 2)

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 27, bottom: 0, right: 27)
    }
}

Here is a picture of what I'm trying to achieve. 这是我想要实现的目标的图片。 Thanks 谢谢

在此输入图像描述

Implement the UICollectionViewFlowLayout 's following methods 实现UICollectionViewFlowLayout的以下方法

public func collectionView(_ collectionView: UICollectionView,
                               layout collectionViewLayout: UICollectionViewLayout,
                               minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }

    public func collectionView(_ collectionView: UICollectionView,
                               layout collectionViewLayout: UICollectionViewLayout,
                               minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }

and for the 27px inset use 并为27px插图使用

yourcollectionView.sectionInset = UIEdgeInsets(top: 10, left: 27, bottom: 10, right: 27)

To use you class which implements this protocol, you have to set the collection view's delegate property. 要使用实现此协议的类,必须设置集合视图的委托属性。

yourcollectionView.delegate = /* object which implement flow layout delegate */

UICollctionViewDelegate automatically conform to UICollectionViewFlowLayoutDelegate , setting the delegate is enough. UICollctionViewDelegate自动符合UICollectionViewFlowLayoutDelegate ,设置委托就足够了。

You are missing another thing while calculating the cell size. 在计算单元格大小时,您会遗漏另一件事。 Your cell size calculation is wrong. 您的单元格大小计算错误。 Implement it as follows 实现如下

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let frameSize = collectionView.frame.size
            let size = (frameSize.width - 64.0) / 2.0 // 27 px on both side, and within, there is 10 px gap.
            return CGSize(width: size, height: size)

    }

If you are using the storyboard to set layout your UI Widget, you can set them in storyboard too. 如果您使用故事板设置UI小部件的布局,您也可以在故事板中设置它们。

故事板解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM