Add UINavigationController inside UIViewController
15,555
Solution 1
Adding a view controller as a child view controller isn't enough. You also need to add the navigation controller's view as a subview of the container view controller's view.
[myNav willMoveToParentViewController:self];
myNav.view.frame = navFrame; //Set a frame or constraints
[self.view addSubview:myNav.view];
[self addChildViewController:myNav];
[myNav didMoveToParentViewController:self];
See the View Controller Programming Guide for more details.
Solution 2
For swift 5
let childNavController = UINavigationController()
parrentVC.addChild(childNavController)
parrentVC.view.addSubview(childNavController.view)
//Add constraints or frame for childNavController here.
childNavController.didMove(toParent: parrentVC)
Related videos on Youtube
Author by
Sony
Updated on June 04, 2022Comments
-
Sony almost 2 years
I have a UIViewController with a UIToolbar (on bottom) and I want to add a UINavigationController with UINavigationBar inside. But the UINavigationController is not displayed.
MyViewController.m :
- (void)viewDidLoad { [super viewDidLoad]; int toolBarHeight = 44; UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, [self.view bounds].size.height-toolBarHeight, [self.view bounds].size.width, toolBarHeight)]; UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:nil]; toolBar.items = @[button]; [self.view addSubview:toolBar]; MyNavigationController *myNav = [[MyNavigationController alloc] init]; [self addChildViewController:myNav]; }
-
HelmiB almost 11 yearsYou need to add viewController inside navigationController.
[[MyNavigationController alloc] initWithRootViewController:viewController];
-
-
Sam over 10 yearsI am doing the same thing, however the height and width of the Nav controller is not as per the frame I set. The origin point is correct though. Can you help?
-
architectpianist over 10 yearsUsually this is related to Auto Layout, not the view controller nesting. You may need to add constraints in code and make sure
translatesAutoresizingMaskIntoConstraints
is set to NO. If you're using springs and struts, make sure you add those before you add the view to the parent view. -
Sam over 10 yearsInitially I was creating a
UINavigationController
programmatically and adding it. I resolved the issue by creating a Container view with the help of IB and connecting it with my UINavigationController. No code added at all, everything done through IB and it works perfect. -
Brian White over 9 yearsI had the same problem as @Sam with the frame height/width. In IB, I created a dummy
UIView
of the size I want and then copy it withmynav.view.frame = parentvc.ncplace.frame
. Xcode says the frame is as expected but it displays wrong. Reading the value back looks correct but if I read it back inviewDidAppear
, it has changed. Maybe because I'm locked in Landscape. I can make it work by setting the frame in that method but then the appearance animation isn't as nice. Instead, I do these steps inviewDidLayoutSubviews
and re-layout, careful not to recurse. That works for me. -
emp over 9 yearsSmall detail: you don't send the [myNav willMoveToParentViewController:self] message. -[UIViewController addChildViewController:] will send the message. Conversely, when removing a child view controller, you need to send [navController willMoveToParentViewController:nil]. Then -removeFromParentViewController will send [navController didMoveToParentViewController:nil].