Skip to content

Instantly share code, notes, and snippets.

@thexande
Created October 30, 2019 02:59
Show Gist options
  • Save thexande/1900100cada4e5e961d9762431647923 to your computer and use it in GitHub Desktop.
Save thexande/1900100cada4e5e961d9762431647923 to your computer and use it in GitHub Desktop.
A window extension to switch root view controllers in an iOS app.
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