UICollectionViewDelegateFlowLayout 插图不起作用

UICollectionViewDelegateFlowLayout insets does not work

我在我的 ViewController 中实现了 UICollectionViewDelegateFlowLayout,并使用以下代码指定插图:

func collectionView(_ collectionView: UICollectionView,
                               layout collectionViewLayout: UICollectionViewLayout,
                                      insetForSectionAt section: Int) -> UIEdgeInsets {
    return sectionInsets
}

但是我用断点调试的时候这个函数一直不执行。 由于我已经将 UICollectionView 的委托指定给 ViewController,我不知道出了什么问题。

要设置集合视图的布局,请在 collectionViewClass 中执行以下操作:

1.实现方法 getLayout:

func getLayout() -> UICollectionViewLayout
    {
        let layout:UICollectionViewFlowLayout =  UICollectionViewFlowLayout()

        layout.itemSize = CGSize(width: 150, height: 150)
        layout.sectionInset = UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25)

        return layout as UICollectionViewLayout

    }

2。从 ViewDidLoad 中的方法设置 属性:

self.collectionView!.collectionViewLayout = self.getLayout()