http://stackoverflow.com/questions/19257999/add-child-view-controller-to-uinavigationcontroller
I'm trying to add a child view controller to a UIViewController contained in a UINavigationController with this code:
- (void)buttonTapped:(id)sender
{
MyChildController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyChild"];
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
viewController.view.alpha = 0.0f;
[UIView animateWithDuration:0.4 animations:^{
viewController.view.alpha = 1.0f;
}];
}
But this is the result:

As you can see the UINavigatioBar and the UIToolbar are still on top of the child view controller. How can I put the child view controller on top of all? I've already tried to replace the code with:
[self.navigationController addChildViewController:viewController];
[self.navigationController.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self.navigationController];
But in this way the viewDidAppear:animated of the viewController doesn't get called, dunno why.
本文详细介绍了在iOS应用中使用UINavigationController时遇到子视图控制器布局问题的解决方法,并通过实例展示了如何正确添加和显示子视图控制器,确保其位于导航栏和工具栏之上。
1370







self.navigationControllertheviewDidAppearmethod is not called? – Fred Collins Oct 8 '13 at 23:21[viewController viewDidAppear:NO]just after the line[viewController didMoveToParentViewController:self.navigationController]. Why the hell the methods are not called? Otherwise if I add the child view controller toselfand notself.navigationControllerit works but as showed in the above image the view is 'inside' thenavigationController. – Fred Collins Oct 8 '13 at 23:44viewWillAppear:andviewDidAppear:to be called, you need to send the message-beginAppearanceTransition:animated:to the view controller responsible for the view you added. Then you do the animation, if any. And in the animation completion block, send-endAppearanceTransitionto the view controller that appeared. NOTE: Do not call[viewController viewDidAppear:NO]- you should never invoke these methods yourself. – Sam Nov 12 '13 at 10:41