How do I reliably get the size of my iPhone view taking rotations into consideration?
Posted
by Sebastian Celis
on Stack Overflow
See other posts from Stack Overflow
or by Sebastian Celis
Published on 2010-03-23T19:32:55Z
Indexed on
2010/03/23
19:33 UTC
Read the original article
Hit count: 342
My application uses a UITabBarController, multiple UINavigationControllers, and supports autorotation. In order to properly layout the subviews within each UIViewController's main view, I really need to know the size available to the UIViewContoller. I need this size to take the UINavigationBar
, the UITabBar
, and the status bar all into account, and thus only return the size available to the content view.
I thought for sure I could use the following from within the UIViewController's code:
CGRect viewControllerBounds = [[self view] bounds];
However, there are a couple of issues with this approach:
The first time the view is loaded,
viewControllerBounds
reports the view as being 320 pixels wide by 460 pixels tall. This is wrong. With a status bar and a navigation bar showing, the height should only be 416 pixels. However, if I rotate the simulator to landscape and then rotate back, the height ofviewControllerBounds
changes to 416.If I rotate the first view in the navigation controller to landscape mode and then push another view controller onto the stack,
viewControllerBounds
for the new view reports a width of 300 pixels and a height of 480 pixels. So the view's bounds didn't even take the rotation into account.
Is there a better way to do this? I really don't want to have to start hardcoding the widths and heights of all the various UI elements the iPhone OS provides. I have tried setting the autoresizing mask of the UIViewController's view, but that doesn't seem to change anything. The views definitely seem to be displaying properly. When I set a background color that view looks like it takes up all of the space available to it.
Any pointers would be greatly appreciated.
© Stack Overflow or respective owner