How can I change the RootViewController in my UIWindow and have it cross dissolve to the new root?
Asked
Active
Viewed 1,275 times
2 Answers
4
I would recommend to use the transitionFromView
method on UIView
. You could use the following code for example to handle your transition then:
- (void)transitionToViewController:(UIViewController *)viewController
withTransition:(UIViewAnimationOptions)transition
{
[UIView transitionFromView:self.window.rootViewController.view
toView:viewController.view
duration:0.65f
options: UIViewAnimationOptionTransitionCrossDissolve
completion:^(BOOL finished){
self.window.rootViewController = viewController;
}];
}
You can find the ref doc of this method here.

tiguero
- 11,477
- 5
- 43
- 61
-
You should also consider using [transitionWithView:duration:options:animations:completion:](http://stackoverflow.com/a/19832157/1721611) as mentioned in this question if you want better control over when the root view controller is set. – nacross Nov 07 '13 at 09:33
0
Let's see my example to know how to switch rootviewcontroller.
- (void)showTutorialsScreen:(BOOL)animated
{
NUDTutorialsViewController *tutorialsVC = [[NUDTutorialsViewController alloc] initWithNibName:NSStringFromClass([NUDTutorialsViewController class]) bundle:nil];
UINavigationController *tutorialsNavController = [[UINavigationController alloc] initWithRootViewController:tutorialsVC];
[self switchRootViewController:tutorialsNavController animated:animated completion:nil];
}
- (void)showLoadingScreen:(BOOL)animated
{
NUDLoadingViewController *loadingVC = [[NUDLoadingViewController alloc] initWithNibName:NSStringFromClass([NUDLoadingViewController class]) bundle:nil];
[self switchRootViewController:loadingVC animated:animated completion:nil];
}
- (void)showMainScreen:(BOOL)animated
{
NUDMainViewController *mainVC = [[NUDMainViewController alloc] initWithNibName:NSStringFromClass([NUDMainViewController class]) bundle:nil];
UINavigationController *mainNavController = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self switchRootViewController:mainNavController animated:animated completion:nil];
}
- (void)switchRootViewController:(UIViewController *)aRootViewController animated:(BOOL)animated completion:(void(^)())completion
{
if (animated) {
[UIView transitionWithView:self.window duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
self.window.rootViewController = aRootViewController;
[UIView setAnimationsEnabled:oldState];
} completion:^(BOOL finished) {
if (completion) completion();
}];
}
else {
self.window.rootViewController = aRootViewController;
if (completion) completion();
}
}

Kiet Nguyen
- 609
- 4
- 8