通过initWithRootViewController以外的方法设置UINavigationController的rootViewController

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

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

你可以通过调用setViewControllers来解决这个setViewControllers

喜欢这个:

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

使用Swift分享知识:

从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 

希望这将有助于某人。

编辑:

用animation改变rootviewcontroller可以实现:

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

我们可以写出与此类似的泛化方法。

这个为我工作,希望它可以帮助你,

 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 

在swift 3.0 xcode8.1

在主界面一般设置中删除:Main < – 这个主界面之后:

 class AppDelegate... var window: UIWindow? fun application... window = UIWindow(frame: UIScreen.main.bounds) window?.makeKeyAndVisible() window?.rootViewController = UINavigationController(rootViewController: NewYourController) 
  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() })