Set rootViewController of UINavigationController by method other than initWithRootViewController

后端 未结 5 798
死守一世寂寞
死守一世寂寞 2020-12-02 11:45

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

I want use

5条回答
  •  广开言路
    2020-12-02 12:16

    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.

    Edited:

    Changing rootviewcontroller With Animation can be achieve with:

     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.

提交回复
热议问题