LuckyLuke
LuckyLuke

Reputation: 49087

Getting height of collection view is wrong after auto layout have done its job

How do I get the height of the collection view in the delegate method so that the cells height are resized? The collection view is not fixed height in my layout so auto layout changes its height, but the cells height is not correct with the current code.

ThiscollectionView.bounds.size.height returns the wrong height it seems. It seems like it is the value before it was resized.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        if indexPath.row % 4 == 0 {
            return CGSize(width: 100, height: collectionView.bounds.size.height)
        } else {
            return CGSize(width: 100, height: collectionView.bounds.size.height)
        }
    }

Upvotes: 1

Views: 1996

Answers (1)

Taier
Taier

Reputation: 2119

You need to wait for ViewDidLayoutSubviews, to get actual size. The problem is -sizeForItemAtIndexPath get calling before that. It's not a great solution, but try to refresh CollectionView cells size after ViewDidLayoutSubviews.

Upvotes: 4

Related Questions