Apple 提供了 UICollectionViewFlowLayout
类,以及一个 UICollectionViewDelegateFlowLayout
协议(protocol),其方法如下
func collectionView(collectionView: UICollectionView,布局collectionViewLayout:UICollectionViewLayout,minimumLineSpacingForSectionAtIndex部分:Int) -> CGFloat
和
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,minimumInteritemSpacingForSectionAtIndex部分:Int)-> CGFloat
我的理解是,默认情况下,UICollectionViewController
将自身设置为 UICollectionViewFlowLayout
的委托(delegate)。所以我假设 UICollectionViewFlowLayout 有一个要设置的委托(delegate)属性。但如果我对它进行子类化,就没有这样的事情了。自动设置委托(delegate)的默认行为是否仍然适用?或者我只是误解了它是如何工作的?
最佳答案
如果你注意的话,你会发现UICollectionViewDelegateFlowLayout
也是一个UICollectionViewDelegate
:
@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>
因此 UICollectionView
委托(delegate)也是 UICollectionViewFlowLayout
委托(delegate)。我对其实现方式的猜测是,布局在内部执行如下操作:
let flowDelegate = self.collectionView.delegate as? UICollectionViewDelegateFlowLayout
let minSpace = flowDelegate?.collectionView(self.collection, layout: self, minimumInteritemSpacingForSectionAtIndex: 0)
关于ios - 为什么 UICollectionViewFlowLayout 子类没有 delegate 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39025035/