Last active
January 4, 2019 16:30
-
-
Save sukov/91709b67226c1ad72cda2e0f2ae137a2 to your computer and use it in GitHub Desktop.
ChangeRootViewController Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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