Forked from serhii-londar/UIWindow change rootViewController
Created
October 11, 2018 08:06
-
-
Save tommypeps/8f468673b07539cf8996db8493b7d4b6 to your computer and use it in GitHub Desktop.
This file contains 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 { | |
/// Fix for http://stackoverflow.com/a/27153956/849645 | |
func set(rootViewController newRootViewController: UIViewController, withTransition transition: CATransition? = nil) { | |
let previousViewController = rootViewController | |
if let transition = transition { | |
// Add the transition | |
layer.add(transition, forKey: kCATransition) | |
} | |
rootViewController = newRootViewController | |
// Update status bar appearance using the new view controllers appearance - animate if needed | |
if UIView.areAnimationsEnabled { | |
UIView.animate(withDuration: CATransaction.animationDuration()) { | |
newRootViewController.setNeedsStatusBarAppearanceUpdate() | |
} | |
} else { | |
newRootViewController.setNeedsStatusBarAppearanceUpdate() | |
} | |
/// The presenting view controllers view doesn't get removed from the window as its currently transistioning and presenting a view controller | |
if let transitionViewClass = NSClassFromString("UITransitionView") { | |
for subview in subviews where subview.isKind(of: transitionViewClass) { | |
subview.removeFromSuperview() | |
} | |
} | |
if let previousViewController = previousViewController { | |
// Allow the view controller to be deallocated | |
previousViewController.dismiss(animated: false) { | |
// Remove the root view in case its still showing | |
previousViewController.view.removeFromSuperview() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment