UICollectionViewFlowLayoutDelegate insetForSectionAt 从未调用过

UICollectionViewFlowLayoutDelegate insetForSectionAt never called

swift3

我需要使用 UICollectionViewDelegateFlowLayout 的一些 optional 方法,但从未调用过下面的方法。

insetForSectionAt

optional public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets

因为 UICollectionViewDelegateFlowLayout 继承自 UICollectionViewDelegate - 我需要做的就是分配 collection'sView 委托:

self.collectionView.delegate = self.delegate

请注意,其他一切正常,并且会调用 sizeForItemcellForItem 等方法。

请确保您是否在 class 中实现了 UICollectionViewDelegateFlowLayout,如下所示:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

//...your code here

}