Search code examples
iphoneiosuiviewcontrolleruinavigationcontroller

Changing height of view inside UINavigationController


I want to change the height of a UIViewController's view that is inside a UINavigationController to display a banner at the bottom so that it doesn't obscure anything.

I thought this would be pretty easy by just changing the view's frame in the viewDidLoad but that didn't work:

CGRect frame = self.view.frame;
self.view.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height - 49.0f);

I also tried to add

[navigationController.view setAutoresizesSubviews:NO];

after initiating the UINavigationController but it still looks the same.

The only option I can think of right now is to use the UINavigationController inside a dummy UITabBarController that will be obscured by the banner but that seems unnecessarily complicated to me.

Is there any way to change the height of the view controller's view inside a UINavigationController?


Solution

  • There's no way to change a view controller's view from within the view controller, but you could use a custom container view controller:

    // Create container
    UIViewController* container = [[UIViewController alloc] init];
    
    // Create your view controller
    UIViewController* myVc = [[MyViewController alloc] init];
    
    // Add it as a child view controller
    [container addChildViewController:myVc];
    [container.view addSubview:myVc.view];
    myVc.view.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleHeight;
    myVc.view.frame = CGRectMake(0, 0, container.view.bounds.size.width, container.view.bounds.size.height-200);
    
    // Add your banner
    UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner"]];
    imgView.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth| UIViewAutoresizingMaskFlexibleTopMargin;
    imgView.frame = CGRectMake(0, container.view.bounds.size.height-200, container.view.bounds.size.width, 200);
    [myVc.view addSubview:imgView];
    

    Now you can add the container view controller to your navigation controller instead of your one.