Swift ios set a new root view controller

前端 未结 20 812
醉话见心
醉话见心 2020-12-13 04:46

I wonder if its possible to set a new root VC?

My app gets init with a uinavigation controller that has a table view to be the root VC.

Then from the table v

相关标签:
20条回答
  • 2020-12-13 04:49

    If you need to set rootViewController with some animations, here is the code:

    guard let window = UIApplication.shared.keyWindow else {
        return
    }
    
    guard let rootViewController = window.rootViewController else {
        return
    }
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "MainTabbar")
    vc.view.frame = rootViewController.view.frame
    vc.view.layoutIfNeeded()
    
    UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
        window.rootViewController = vc
    }, completion: { completed in
        // maybe do something here
    })
    
    0 讨论(0)
  • 2020-12-13 04:50

    Swift 3 AppDelegate file:::

    @IBAction func btnGoBack(_ sender: UIButton){
    
       self.goToHomeVC()
    }
    
    func goToHomeVC(){
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
        let viewController = storyboard.instantiateViewController(withIdentifier :"HomeVC") as! HomeVC
    
        let navController = UINavigationController.init(rootViewController: viewController)
    
        if let window = self.appDelegate.window, let rootViewController = window.rootViewController {
    
            var currentController = rootViewController
    
            while let presentedController = currentController.presentedViewController {
    
                currentController = presentedController
            }
    
            currentController.present(navController, animated: true, completion: nil)
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:51

    You can try out this code

    func switchRootViewController(rootViewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
        guard let window = UIApplication.shared.keyWindow else { return }
        if animated {
            UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: {
                let oldState: Bool = UIView.areAnimationsEnabled
                UIView.setAnimationsEnabled(false)
                window.rootViewController = rootViewController
                UIView.setAnimationsEnabled(oldState)
            }, completion: { (finished: Bool) -> () in
                if (completion != nil) {
                    completion!()
                }
            })
        } else {
            window.rootViewController = rootViewController
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:52

    Swift 4.2

    May be you should try this

    let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
    let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "respectiveIdentifier") as! ViewController
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = redViewController
    
    0 讨论(0)
  • 2020-12-13 04:52

    For swift 4.0.

    In your AppDelegate.swift file in didfinishedlaunchingWithOptions method, put the following code.

    var window: UIWindow?
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
    
        let rootVC = MainViewController() // your custom viewController. You can instantiate using nib too. UIViewController(nib name, bundle)
    
        let navController = UINavigationController(rootViewController: rootVC) // Integrate navigation controller programmatically if you want
        window?.rootViewController = navController
    
        return true
    }
    

    Hope it will work just fine.

    0 讨论(0)
  • 2020-12-13 04:52

    Any view controller you want to set root just call the below function like

      UIApplication.shared.setRootVC(vc)
    
      extension UIApplication {
    
        func setRootVC(_ vc : UIViewController){
    
            self.windows.first?.rootViewController = vc
            self.windows.first?.makeKeyAndVisible()
    
          }
      }
    
    0 讨论(0)
提交回复
热议问题