Determine the correct size in loadView

RickiG picture RickiG · Sep 20, 2011 · Viewed 9.3k times · Source

How to consistently determine the correct size to layout your view in loadView?

I have different cases where a UIViewController is pushed to a UINavigationController, or a UITabBarController is present, or both. These elements changes the available screen area and makes it inconsistent if I hard code values.

At the moment where loadView is called and I want to know how to size the container view I add my subviews to.

I looked through the UIViewController guide and Apple uses UIScreen mainScreen applicationFrame, but in their example this is done because they are making a full screen app.

I have seen some answers in here, but none that addresses how to do this in a consistent manner.

Thanks for any help given.

Answer

Javier Soto picture Javier Soto · Sep 7, 2013

You don't need to know the exact size. A VC shouldn't have to size itself, that's the responsibility of its parent. Just create a UIView with [[UIView alloc] init] as confirmed by Apple UIKit Engineer Andy Matuschak on Twitter: https://twitter.com/andy_matuschak/status/365227892151558144