Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 376
Bryan Irace Avatar asked Nov 24 '12 18:11

Bryan Irace


People also ask

How do I add a view controller to my navigation controller?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .

Which method should you call on a parent UIViewController when adding a child UIViewController?

The delegation pattern in your link is the correct way to do it, and it works just fine when you use ViewControllers.


1 Answers

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);
like image 172
Bryan Irace Avatar answered Sep 20 '22 02:09

Bryan Irace