cweber105 cweber105 - 3 months ago 25
iOS Question

Why doesn't a UICollectionViewFlowLayout subclass have a delegate property?

Apple provides the

UICollectionViewFlowLayout
class, and along with it, a
UICollectionViewDelegateFlowLayout
protocol, with methods like

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat


and

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat


My understanding is that, by default, a
UICollectionViewController
sets itself as the delegate of the
UICollectionViewFlowLayout
. So I assume
UICollectionViewFlowLayout
has a delegate property to be set. But if I subclass it, there is no such thing. Does the default behavior still apply, where the delegate is set automatically? Or am I just misunderstanding how this works?

Answer

If you pay attention you'll see that UICollectionViewDelegateFlowLayout is also a UICollectionViewDelegate:

@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>

So the UICollectionView delegate is also the UICollectionViewFlowLayout delegate. My guess on how this is implemented is that internally the layout does something like this:

let flowDelegate = self.collectionView.delegate as? UICollectionViewDelegateFlowLayout
let minSpace = flowDelegate?.collectionView(self.collection, layout: self, minimumInteritemSpacingForSectionAtIndex: 0)