9

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];
}
1
  • 3
    You need to add viewController inside navigationController. [[MyNavigationController alloc] initWithRootViewController:viewController]; Commented Jul 22, 2013 at 1:57

2 Answers 2

17

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I 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?
Usually 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.
Initially 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.
I 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 with mynav.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 in viewDidAppear, 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 in viewDidLayoutSubviews and re-layout, careful not to recurse. That works for me.
Small 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].
1

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)
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.