Get width and height of container from view
Asked Answered
C

1

5

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?

Currant answered 27/8, 2014 at 4:51 Comment(3)
self.view.bounds.size.height & self.view.bounds.size.width.Milli
From the interface builder the dimensions are 320 x 250. This is what I am expecting.Currant
Yes that's what I'm using to get the respective width and height. (Yields the above results) Just thought I'd save some typing.Currant
C
12

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.

Currant answered 27/8, 2014 at 5:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.