用 initWithRootViewController 以外的方法设置 UINavigationController 的 rootViewController

如何通过 initWithRootViewController以外的方法设置 UINavigationControllerrootViewController

我想使用 initWithNavigationBarClass:toolbarClass:为我的 NavigationController 提供一个自定义工具栏,所以我不认为我可以使用 initWithRootViewController

108302 次浏览

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)

Knowledge Sharing Using Swift:

Changing root view controller from class other than app delegate.swift

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav

Hope this will helpful for someone.

Changing rootviewcontroller With Animation can be achieved with:

UIView.transitionWithView(self.window!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
self.window?.rootViewController = anyViewController
}, completion: nil)

We can write a generalised method too, similar to this.

  let storyboard = UIStoryboard(name: "Main", bundle: nil)
let yourNewRootView = storyboard.instantiateViewControllerWithIdentifier("yourNewRootView") as? yourNewRootView




self.window = UIWindow(frame: UIScreen.mainScreen().bounds)


UIView.transitionWithView(self.window!, duration: 0.1, options: [UIViewAnimationOptions.TransitionFlipFromRight,UIViewAnimationOptions.TransitionFlipFromLeft], animations:
{
// animation


}, completion: { (finished: Bool) -> () in


self.window?.rootViewController = nil
self.window?.rootViewController = yourNewRootView
self.window?.makeKeyAndVisible()
})

this one works for me, hope it helps you,

let rootVC:LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
let nvc:UINavigationController = self.storyboard?.instantiateViewControllerWithIdentifier("RootNavigationController") as! UINavigationController
nvc.viewControllers = [rootVC]
UIApplication.sharedApplication().keyWindow?.rootViewController = nvc

In swift 3.0 xcode8.1

in general settings delete in Main Interface: Main <-this after Main Interface:

class AppDelegate...


var window: UIWindow?


fun application...


window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: NewYourController)