sebastien
sebastien

Reputation: 2519

UICollectionView not the right size in viewDidLoad

I have a simple UICollectionView defined in my storyboard (see screenshot).

enter image description here

I'm using auto layout, the UICollectionView is attached to the superview for leading, trailing and top and to the toolbar for the bottom.

When I display in the console the size of the UICollectionView in viewDidLoad with:

NSLog(@"%s CollectionView ========== > height:%f width:%f", __PRETTY_FUNCTION__,self.theCollectionView.bounds.size.height, self.theCollectionView.bounds.size.width);

I get: CollectionView ========== > Frame height:492.000000 width:600.000000

This size is the one which is also displayed in the storyboard (Width:600 Height:492).

When I run the same log in viewDidAppear, I get: CollectionView ========== > Frame height:663.000000 width:1024.000000

which is what I expect. I'm surprise to have the appropriate size only in the viewDidAppear, please can you explain or is there something wrong in my storyboard?

Thanks,

Sébastien.

Upvotes: 2

Views: 1193

Answers (1)

tanner0101
tanner0101

Reputation: 4065

For anything frame size dependent, I usually use

override func viewDidLayoutSubviews()

This will take into consideration Auto Rotate as well.

Upvotes: 5

Related Questions