Set rootViewController of UINavigationController by method other than initWithRootViewController

drc picture drc · Apr 25, 2013 · Viewed 96.7k times · Source

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.

Answer

Leon Lucardie picture Leon Lucardie · Apr 25, 2013

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)