Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get width and height of container from view

Forgive me if this is a duplicate. I thought for sure this would be an obvious question.

I have a container in a storyboard, which is of class ModelViewController (the whole view controller). From ModelViewController I need to get the width and height of the container. I am currently trying:

self.view.bounds.size

Which yields 0.563380 and 0.000000. I also tried

self.view.frame.size

Which yields the dimensions of the entire screen. What am I missing here?

like image 391
Orange Peel Avatar asked Aug 27 '14 04:08

Orange Peel


1 Answers

From this post

View frames are not actually useable in viewDidLoad; you should move all of your geometry-manipulating code into viewWillAppear. The system will clobber any changes you set in viewDidLoad between there and when it's about tom come on screen.

And the comment

Actually, you should move geometry changes to -viewDidAppear pre-iOS 5 and to -viewWillLayoutSubviews for iOS 5+. -viewWillAppear: may not actually have the correct bounds/orientation if the view is being animated.

I found the solution. The correct dimensions are indeed given when called from viewWillLayoutSubviews. I was calling from viewDidLoad giving incorrect results.

like image 95
Orange Peel Avatar answered Oct 20 '22 01:10

Orange Peel