IOS - Changing the root view controller with animation

I am changing the root view controller by pressing a button that connects to the custom segue view controller that I want to go to. Custom segue looks like this:

- (void)perform{ UIViewController *source = (UIViewController *)self.sourceViewController; source.view.window.rootViewController = self.destinationViewController; } 

But that immediately changes the root view controller. I would like the controller to disappear at the top of the old controller.

+8
ios objective-c segue
source share
4 answers

The general way to do this is:

  UIStoryboard *sb = [UIStoryboard storyboardWithName:@"YourSBName" bundle:nil]; UIViewController *vc = [sb instantiateInitialViewController];// Or any VC with Id DictateITAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; appDelegate.window.rootViewController = vc; // PLEASE READ NOTE ABOUT THIS LINE [UIView transitionWithView:appDelegate.window duration:INTERVAL_DURATION options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ appDelegate.window.rootViewController = vc; } completion:nil]; 

Now a few notes about:

 appDelegate.window.rootViewController = vc; // PLEASE READ NOTE ABOUT THIS LINE 

The new view controller that you create will be presented in the default orientation, that is, in the portrait. Therefore, it is possible that if you do this in the landscape, your new controller will appear as a portrait, and then turn into a landscape. This line fixed this problem for me.

+20
source share

I think there are many answers for this, but something like this should work:

 [UIView transitionWithView:self.sourceViewController.view.window duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.sourceViewController.view.window.rootViewController = self.destinationViewController; } completion:^(BOOL finished) { // Code to run after animation }]; 
+3
source share

I needed to translate this to Swift 3.0 for my current project. Here's how to do it:

 //Get the storyboard that contains the VC you want let storyboard = UIStoryboard(name: "Main", bundle: nil) //Get the VC you want to push onto the stack //You can use storyboard.instantiateViewController(withIdentifier: "yourStoryboardId") guard let vc = storyboard.instantiateInitialViewController() else { return } //Get the current app delegate guard let appDel = UIApplication.shared.delegate as? AppDelegate else { return } //Set the current root controller and add the animation with a //UIView transition appDel.window?.rootViewController = vc UIView.transition(with: appDel.window!, duration: 0.25, options: .transitionCrossDissolve, animations: { appDel.window?.rootViewController = vc } , completion: nil) 
+2
source share

The easiest way:

[sourceViewController presentViewController:destinationViewController animated:YES completion:nil];

More on this: https://developer.apple.com/library/ios/featuredarticles/viewcontrollerpgforiphoneos/ModalViewControllers/ModalViewControllers.html

-3
source share

All Articles