Skip to content

Instantly share code, notes, and snippets.

@nvkiet
Last active January 28, 2025 07:46
Show Gist options
  • Save nvkiet/6368d1d45c4ea3e6d9cb to your computer and use it in GitHub Desktop.
Save nvkiet/6368d1d45c4ea3e6d9cb to your computer and use it in GitHub Desktop.
Switch root view controller
func switchRootViewController(rootViewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
if animated {
UIView.transitionWithView(window, duration: 0.5, options: .TransitionCrossDissolve, animations: {
let oldState: Bool = UIView.areAnimationsEnabled()
UIView.setAnimationsEnabled(false)
self.window!.rootViewController = rootViewController
UIView.setAnimationsEnabled(oldState)
}, completion: { (finished: Bool) -> () in
if completion {
completion!()
}
})
} else {
window!.rootViewController = rootViewController
}
}
@alkozin
Copy link

alkozin commented Jan 28, 2025

Thanks for great snippet, mate! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment