Tonespy
Tonespy

Reputation: 3387

Getting CollectionViewContentSize

I am trying to get CollectionViewContentSize as done in apple's Photo objective C implementation. Below is the codes

Apple's Code

 CGSize cellSize = ((UICollectionViewFlowLayout *)self.collectionViewLayout).itemSize;

My Code

 let cellSize: CGSize = UICollectionViewFlowLayout.collectionViewContentSize(self.collectionViewLayout)

And I get an error saying (UICollectionViewLayout) -> 'is not convertible to 'UICollectionViewFlowLayout -> () -> CGSize'

Upvotes: 0

Views: 596

Answers (2)

katleta3000
katleta3000

Reputation: 2494

It's easy: self.collectionViewLayout return UICollectionViewLayout type not UICollectionViewFlowLayout, so you should cast it:

if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    let cellSize: CGSize = UICollectionViewFlowLayout.collectionViewContentSize(layout)()
}

Upvotes: 2

tuledev
tuledev

Reputation: 10317

ObjC

  CGSize cellSize = ((UICollectionViewFlowLayout *)self.collectionViewLayout).itemSize;

Swift

  let cellSize: CGSize = self.collectionViewLayout.itemSize

Upvotes: 0

Related Questions