Search code examples
swiftuicollectionviewlayoutcontentsize

Swift UICollectionViewLayout, whither contentSize


I'm converting my app from Obj-C to Swift.

I have this subclass of UICollectionViewLayout that overrides 'prepareForLayout' which has been renamed to 'prepare'. So far so good. However, in my obj-c code, I have this line:

self.contentSize = CGSizeMake(contentWidth, contentHeight+40);

where 'self' refers to the custom UICollectionViewLayout.

Now, in Swift, when I try to do

self.contentSize = CGSize(width: contentWidth, height: contentHeight+40)

I get this error claiming

Value of type 'myCustomCollectionLayout' has no member 'contentSize'

I do know that there is something like collectionViewContentSize but that doesn't seem to be appropriate here.

I hope any of you could advise me on how to proceed


Solution

  • contentSize is not UICollectionViewLayout property, it is property on UICollectionView or UIScrollView.

        collectionView?.contentSize = CGSize(width: contentWidth, height: contentHeight+40)
    

    But, why would you require this, in collectionview layout. You should really consider overriding the property collectionViewContentSize, which is exactly what contentSize does for UIScrollView,

    override var collectionViewContentSize: CGSize {
        return CGSize(width: contentWidth, height: contentHeight+40) 
    }