Set frame of a navigation controller added as a child view controller

Bryan Irace picture Bryan Irace · Nov 24, 2012 · Viewed 8.7k times · Source

I am trying to add a UINavigationController as a child view controller and then give it a smaller frame than its parent. However, changing the navigation controller's frame does not properly change the frame of the navigation controller's root view controller.

In viewDidLoad

RootController *rootController = [[RootController alloc] init];
_navController = [[UINavigationController alloc] initWithRootViewController:rootController];
[rootController release];

[self addChildViewController:_navController];
[self.view addSubview:_navController.view];
[_navController didMoveToParentViewController:self];

Then, in viewWillAppear::

CGRect bounds = self.view.bounds;
bounds.origin.x = 20;
bounds.origin.y = 20;
bounds.size.width = bounds.size.width - 20;
bounds.size.height = bounds.size.height - 20;
_navController.view.bounds = bounds;

While the navigation bar is properly placed, the white background of the root view controller is not. What am I doing wrong exactly? Thanks for your help. enter image description here

Answer

Bryan Irace picture Bryan Irace · Nov 25, 2012

It actually works if you do this in viewDidLoad immediately after adding the child view controller to the parent:

_navController.view.frame = CGRectInset(self.view.bounds, 20, 20);