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)
    
Share:
15,555

Related videos on Youtube

Sony
Author by

Sony

Updated on June 04, 2022

Comments

  • Sony
    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
      HelmiB almost 11 years
      You need to add viewController inside navigationController. [[MyNavigationController alloc] initWithRootViewController:viewController];
  • Sam
    Sam over 10 years
    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?
  • architectpianist
    architectpianist over 10 years
    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.
  • Sam
    Sam over 10 years
    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.
  • Brian White
    Brian White over 9 years
    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.
  • emp
    emp over 9 years
    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].