Search code examples
iosswiftuicollectionviewuicollectionviewcelluicollectionviewlayout

collectionViewContentSize() vs contentSize


What is the difference between

collectionViewController.collectionViewLayout.collectionViewContentSize() and collectionViewController.collectionView.contentSize ?

What do you prefer to use?


Solution

  • collectionViewContentSize() is a method you can override (in the layout) to generate the size dynamically.

    contentSize is a property of collectionView (or any UIScrollView, for that matter) that is going to be used if there is no such override.

    It is similar to a UITableView's rowHeight vs. the UITableViewDelegate's heightForRowAtIndexPath().

    Please also note (as mentioned in the comments) that "contentSize is a UIScrollView-inherited property, while itemSize is the property on UICollectionViewLayout that allows you to specify a blanket size for each cell".