Skip to content

Instantly share code, notes, and snippets.

@sukov
Last active January 4, 2019 16:30
Show Gist options
  • Save sukov/91709b67226c1ad72cda2e0f2ae137a2 to your computer and use it in GitHub Desktop.
Save sukov/91709b67226c1ad72cda2e0f2ae137a2 to your computer and use it in GitHub Desktop.
ChangeRootViewController Swift
extension UIViewController {
func changeRootViewController(to viewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
let toVC = viewController
let snapshot = UIApplication.shared.keyWindow!.snapshotView(afterScreenUpdates: true)!
let rootViewControllerChangeBlock = {
toVC.view.addSubview(snapshot)
UIApplication.shared.keyWindow?.rootViewController = toVC
let duration = animated ? 0.3 : 0
UIView.animate(withDuration: duration, animations: {
snapshot.layer.opacity = 0
snapshot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5)
}, completion: { _ in
snapshot.removeFromSuperview()
completion?()
})
}
if UIApplication.shared.keyWindow?.rootViewController?.presentedViewController != nil {
UIApplication.shared.keyWindow?.addSubview(snapshot)
UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: false, completion: {
rootViewControllerChangeBlock()
})
} else {
rootViewControllerChangeBlock()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment