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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16215034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:20:21  来源:igfitidea点击:

Set rootViewController of UINavigationController by method other than initWithRootViewController

iosuinavigationcontrolleruitoolbariphone-5

提问by drc

How Do I set the rootViewControllerof UINavigationControllerby a method other than initWithRootViewController?

如何通过除 之外的方法设置rootViewControllerof ?UINavigationControllerinitWithRootViewController

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

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

回答by Leon Lucardie

You can solve this by calling setViewControllers.

您可以通过调用来解决此问题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)

回答by Arvind

Knowledge Sharing Using Swift:

使用 Swift 进行知识共享:

Changing root view controller from class other than app delegate.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

Hope this will helpful for someone.

希望这对某人有帮助。

Edited:

编辑:

Changing rootviewcontroller With Animation can be achieve with:

使用动画更改 rootviewcontroller 可以通过以下方式实现:

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

We can write generalise method too similar to this.

我们可以编写与过于相似的泛化方法

回答by Patel Jigar

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

回答by Anton Russia

In swift 3.0 xcode8.1

在 swift 3.0 xcode8.1

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

在主界面中的一般设置中删除: Main <-主界面后:

class AppDelegate...

 var window: UIWindow?

    fun application...

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

回答by idris y?ld?z

  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()
    })