5

I have several UIViewControllers, loaded via a UINavigationController, where I override loadView, and setup a custom view to display. I am having problems with setting the frames of some of the subviews, and maybe the view itself. I think that either the UINavigationController or the UIViewController is able to force it's view to fill the screen when displayed.

I am wondering if and/or when the size of the view is set. And if it is happening, is it setting the frame of the view, or doing it another way.

Thanks.

Update: I have just noticed while working through this is that I make an new UINavigationController, with this loadView method:

- (void)loadView
{
  UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 10.0f, 10.0f)];
  [view setBackgroundColor:[UIColor redColor]];
  self.view = view;
  [view release];
}

I get a red view taking up the entire screen, except for the status bar and header. When does my view get resized to fill the screen? And what size should I use when creating the view?

2 Answers 2

9

UINavigationController will automagically size a ViewController's view when pushed. From the docs:

pushViewController:animated:

The object in the viewController parameter becomes the top view controller on the navigation stack. Pushing a view controller results in the display of the view it manages. How that view is displayed is determined by the animated parameter. If the animated parameter is YES, the view is animated into position; otherwise, the view is simply displayed in place. The view is automatically resized to fit between the navigation bar and toolbar (if present) before it is displayed.

If you want to have a smaller view displayed then you'll have to create a container and make it a child of your VC's view, setting the size manually. The autoresize mask may have to be set properly, depending on how you create the view.

3
  • Thanks, this must have been the documentation I read once but forgot exactly what/where I read it.
    – Ryan
    Oct 18, 2011 at 22:28
  • 2
    Is there any way to force the navigation controller to readjust its child view controller's size after the push operation? I am changing the height of the navigation controller but it is not automatically adjusting its contents for some reason.
    – devios1
    Jun 3, 2014 at 0:37
  • I'm also seeing an issue where the navigation controller isn't resizing its child view controller's size when you rotate the device during the transition.
    – AdamPro13
    Oct 14, 2015 at 18:51
3

The framespace for the UIViewControllers becomes smaller, because UINavigationController manipulates the view by adding a UINavigationBar, which is 44.0f in size.

It gets resized because you replace the view with self.view = view; If you would have used addSubview: instead it would have stayed it's original size. logancautrell explained when and why.

6
  • Thanks, for the clarification. When loading my view in a view controllers loadView, when pushed via a UINavigationController, do I just need to set the frame as CGRectMake( 0.0, 0.0, 320.0, 416.0)? As in I don't need to compensate in the frame origin y coordinate for the UINavigationBar or the Status bar?
    – Ryan
    Oct 18, 2011 at 20:47
  • The bottom of the NavBar becomes the new X,Y origin for the viewcontrollers, you only need to compensate for the adjusted height. To be sure of the dimensions use NSLog(@"w x h = %f x %f",self.view.frame.size.width, self.view.frame.size.height);
    – Wolfert
    Oct 18, 2011 at 21:13
  • Thanks, that clarifies it for me. I am still wondering about when the view gets changed. Because even if I set the incorrect size in loadView it gets changed to be the correct size. I will accept you answer, since it gets me going. Thanks again.
    – Ryan
    Oct 18, 2011 at 21:23
  • It gets resized here: self.view = view;
    – Wolfert
    Oct 18, 2011 at 21:29
  • If instead you would use: [self.view addSubview:view]; It would not resize
    – Wolfert
    Oct 18, 2011 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.