23
votes

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.

4
Why not use auto resizing masks?Krumelur

4 Answers

16
votes

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

13
votes
- (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.

0
votes

The "right" answer is probably to create a custom UIView subclass and do all of the subview view positioning in your override of layoutSubviews. Your subclass would then get assigned to self.view in loadView.

However, it also works to use viewDidLoad to add a subview of identical size to self.view. (Though you should be prepared for this view to change size. -- Correctly setting autoresizingMasks can be a good way to do this.)

-1
votes

I have made an app, it loads several UITextViews, UIButtons, and a UISegment control. What I did in viewWillAppear, I call a resize method I made.

It changes the sizes of all subviews in the UIViewController programmatically. This is because autoresizing in IB is only so good.

In my calcViewSize function, I get the dimensions like this;

CGFloat width = [[UIScreen mainScreen] applicationFrame].size.width;
CGFloat height = self.view.frame.size.height; //this will retrieve the frame we are drawn into.

Then I walk through each subview in the UIViewController that I want to resize, and change its frame. I have properties for these views in IBOutlets, so I already know what the views are.

Use Interface Builder to get the dimensions.

One last Note: To change dimensions in IB, select the View, select attributes inspector, disable the Status Bar, now in Size Inspector, you can resize the view, re-arrange your subviews, and write down the numbers to place in your code.