Option 1 Swap the Root View Controller Good
suggest changeThere are benefits to switching the root view controller, although the transition options are limited to those supported by UIViewAnimationOptions
, so depending on how you wish to transition between flows might mean you have to implement a custom transition - which can be cumbersome.
You can show the Onboarding flow by simply setting the UIApplication.shared.keyWindow.rootViewController
Dismissal is handled by utilizing UIView.transition(with:)
and passing the transition style as a UIViewAnimationOptions
, in this case Cross Dissolve. (Flips and Curls are also supported).
You also have to set the frame of the Main view before you transition back to it, as you’re instantiating it for the first time.
// MARK: - Onboarding
extension AppDelegate {
func showOnboarding() {
if let window = UIApplication.shared.keyWindow, let onboardingViewController = UIStoryboard(name: "Onboarding", bundle: nil).instantiateInitialViewController() as? OnboardingViewController {
onboardingViewController.delegate = self
window.rootViewController = onboardingViewController
}
}
func hideOnboarding() {
if let window = UIApplication.shared.keyWindow, let mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() {
mainViewController.view.frame = window.bounds
UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: {
window.rootViewController = mainViewController
}, completion: nil)
}
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents