Skip to content

Instantly share code, notes, and snippets.

@virasio
Last active March 8, 2016 07:37
Show Gist options
  • Save virasio/7ff2d5b852633c1712a7 to your computer and use it in GitHub Desktop.
Save virasio/7ff2d5b852633c1712a7 to your computer and use it in GitHub Desktop.
Method replace current root view controller to new view controller with animation and different orientations
//
// UIViewController+ReplaceRoot.h
//
// Created by Victor Surikov on 08/03/16.
// Copyright © 2016 Victor Surikov. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIViewController (ReplaceRoot)
+ (void)replaceCurrentRootViewControllerTo:(UIViewController *)newViewController;
@end
//
// UIViewController+ReplaceRoot.m
//
// Created by Victor Surikov on 08/03/16.
// Copyright © 2016 Victor Surikov. All rights reserved.
//
#import "UIViewController+ReplaceRoot.h"
BOOL OrientationMaskSupportsOrientation(UIInterfaceOrientationMask mask, UIInterfaceOrientation orientation) {
return (mask & (1 << orientation)) != 0;
}
@implementation UIViewController (ReplaceRoot)
+ (void)replaceCurrentRootViewControllerTo:(UIViewController *)newViewController {
id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
UIViewController *currentViewController = appDelegate.window.rootViewController;
CGRect currentRect = currentViewController.view.frame;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
BOOL orientationSupported = OrientationMaskSupportsOrientation(
newViewController.supportedInterfaceOrientations,
interfaceOrientation);
CGRect newRect = orientationSupported
? CGRectMake(0, 0, currentRect.size.width, currentRect.size.height)
: CGRectMake(0, 0, currentRect.size.height, currentRect.size.width);
newViewController.view.frame = newRect;
[newViewController.view layoutIfNeeded];
UIImage *newViewControllerAsImage = [self imageWithView:newViewController.view];
if (!orientationSupported) {
UIImageOrientation newImageOrientation;
switch (interfaceOrientation) {
case UIInterfaceOrientationPortrait:
if (OrientationMaskSupportsOrientation(newViewController.supportedInterfaceOrientations, UIInterfaceOrientationLandscapeLeft)) {
newImageOrientation = UIImageOrientationRight;
} else if (OrientationMaskSupportsOrientation(newViewController.supportedInterfaceOrientations, UIInterfaceOrientationLandscapeRight)) {
newImageOrientation = UIImageOrientationLeft;
} else {
newImageOrientation = UIImageOrientationDown;
}
break;
case UIInterfaceOrientationPortraitUpsideDown:
if (OrientationMaskSupportsOrientation(newViewController.supportedInterfaceOrientations, UIInterfaceOrientationLandscapeLeft)) {
newImageOrientation = UIImageOrientationLeft;
} else if (OrientationMaskSupportsOrientation(newViewController.supportedInterfaceOrientations, UIInterfaceOrientationLandscapeRight)) {
newImageOrientation = UIImageOrientationRight;
} else {
newImageOrientation = UIImageOrientationDown;
}
break;
case UIInterfaceOrientationLandscapeRight:
if (OrientationMaskSupportsOrientation(newViewController.supportedInterfaceOrientations, UIInterfaceOrientationPortrait)) {
newImageOrientation = UIImageOrientationLeft;
} else if (OrientationMaskSupportsOrientation(newViewController.supportedInterfaceOrientations, UIInterfaceOrientationPortraitUpsideDown)) {
newImageOrientation = UIImageOrientationRight;
} else {
newImageOrientation = UIImageOrientationDown;
}
break;
case UIInterfaceOrientationLandscapeLeft:
if (OrientationMaskSupportsOrientation(newViewController.supportedInterfaceOrientations, UIInterfaceOrientationPortrait)) {
newImageOrientation = UIImageOrientationRight;
} else if (OrientationMaskSupportsOrientation(newViewController.supportedInterfaceOrientations, UIInterfaceOrientationPortraitUpsideDown)) {
newImageOrientation = UIImageOrientationLeft;
} else {
newImageOrientation = UIImageOrientationDown;
}
break;
default:
newImageOrientation = UIImageOrientationUp;
}
newViewControllerAsImage = [[UIImage alloc] initWithCGImage:newViewControllerAsImage.CGImage
scale:1.0
orientation:newImageOrientation];
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:newViewControllerAsImage];
imageView.backgroundColor = [UIColor whiteColor];
[appDelegate.window addSubview:imageView];
imageView.frame = CGRectMake(
currentRect.origin.x,
currentRect.origin.y + currentRect.size.height,
currentRect.size.width,
currentRect.size.height
);
[UIView animateWithDuration:1.0
animations:^{
imageView.frame = currentRect;
}
completion:^(BOOL finished) {
if (finished) {
appDelegate.window.rootViewController = newViewController;
[appDelegate.window makeKeyAndVisible];
}
}];
}
#pragma mark Private helper methods
+ (UIImage *)imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment