Last active
January 28, 2025 07:46
-
-
Save nvkiet/6368d1d45c4ea3e6d9cb to your computer and use it in GitHub Desktop.
Switch root view controller
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
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 | |
} | |
} |
Thanks for great snippet, mate! 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Crash Occurrence in this method, not regular. Any Idea to fix this sporadic crash..
NSInternalInconsistencyException - Attempting to transfer an animation to an animation state that is not a direct child of the animation's animation state.
Fatal Exception: NSInternalInconsistencyException
0 CoreFoundation 0x92d1c __exceptionPreprocess
1 libobjc.A.dylib 0x14ee4 objc_exception_throw
2 Foundation 0x124c80 _userInfoForFileAndLine
3 UIKitCore 0x5f0658 -[UIViewAnimationState _transferAnimationToTrackingAnimator:]
4 UIKitCore 0x71ee0 __85+[UIViewPropertyAnimator _animationBlockForTrackingAnimation:animator:trackingSetup:]_block_invoke
5 UIKitCore 0x4a3d14 -[UIViewPropertyAnimator _runAnimations]
6 UIKitCore 0x34fba4 __49-[UIViewPropertyAnimator startAnimationAsPaused:]_block_invoke_2
7 UIKitCore 0x206ccc __49-[UIViewPropertyAnimator startAnimationAsPaused:]_block_invoke
8 UIKitCore 0x70754c __49-[UIViewPropertyAnimator startAnimationAsPaused:]_block_invoke.1011
9 UIKitCore 0x510eac -[UIViewPropertyAnimator _setupAnimationTracking:]
10 UIKitCore 0x9c494 -[UIViewPropertyAnimator startAnimationAsPaused:]
11 UIKitCore 0x3396e4 +[UIViewPropertyAnimator _trackAnimationWithAnimator:forLayer:forAnimationKey:trackingSetup:]
12 UIKitCore 0x9c07c +[UIViewPropertyAnimator _trackNonAdditiveAnimationWithAnimator:forLayer:forKey:]
13 UIKitCore 0x58d55c -[UIViewAnimationState pop]
14 UIKitCore 0x25bdb8 +[UIViewAnimationState popAnimationState]
15 UIKitCore 0x1751a8 +[UIView _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:]