Created
October 30, 2019 02:59
-
-
Save thexande/1900100cada4e5e961d9762431647923 to your computer and use it in GitHub Desktop.
A window extension to switch root view controllers in an iOS app.
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 UIWindow { | |
func switchRootViewController(_ viewController: UIViewController, | |
animated: Bool = true, | |
duration: TimeInterval = 0.5, | |
options: UIView.AnimationOptions = .transitionFlipFromBottom, | |
completion: (() -> Void)? = nil) { | |
guard animated else { | |
rootViewController = viewController | |
return | |
} | |
UIView.transition(with: self, duration: duration, options: options, animations: { | |
let oldState = UIView.areAnimationsEnabled | |
UIView.setAnimationsEnabled(false) | |
self.rootViewController = viewController | |
UIView.setAnimationsEnabled(oldState) | |
}) { _ in | |
completion?() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment