iosuinavigationcontrolleruitoolbariphone-5

Set rootViewController of UINavigationController by method other than initWithRootViewController


How Do I set the rootViewController of UINavigationController by a method other than initWithRootViewController?

I want use initWithNavigationBarClass:toolbarClass: to deliver a custom toolbar for my NavigationController, so I don't think I can use initWithRootViewController.


Solution

  • You can solve this by calling setViewControllers.

    Like this:

    UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];
    
    [navigationController setViewControllers:@[yourRootViewController] animated:NO];
    

    Swift version:

    let navigationController = UINavigationController(navigationBarClass: MyNavigationBar.self, toolbarClass: UIToolbar.self)
    
    navigationController.setViewControllers([yourRootViewController], animated: false)