Swift ios set a new root view controller

前端 未结 20 912
醉话见心
醉话见心 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
    })
    

提交回复
热议问题