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.
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 .
The delegation pattern in your link is the correct way to do it, and it works just fine when you use ViewControllers.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With