iosxcodecontainer-view

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?


Solution

  • 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.