Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the correct size in loadView

Tags:

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.

like image 431
RickiG Avatar asked Sep 20 '11 13:09

RickiG


2 Answers

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

like image 163
Javier Soto Avatar answered Oct 16 '22 02:10

Javier Soto


- (void) loadView {      //self.wantsFullScreenLayout = YES; //could add this for translucent status bars      UIView *view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];      self.view = view; } 

From Apple's View Controller Programming Guide for creating view programmatically:

  1. Create a root view object that is sized to fit the screen. The root view acts as the container for all other views associated with your view controller. You typically define the frame for this view to match the size of the application window, which itself should fill the screen. However, the view controller also adjusts the frame size as needed to accommodate the presence of assorted views, such as the system status bar, a navigation bar, or a tab bar. You can use a generic UIView object, a custom view you define, or any other view that can scale to fill the screen.

Then it has an example which is similar to the one I've written above.

like image 20
bandejapaisa Avatar answered Oct 16 '22 03:10

bandejapaisa