nganphant
9/28/2017 - 6:17 AM

Prevent all screen from rotation but specific screen.

Steps: ① Add below methods to App Delegate ② In the screen that will be rotated, add an empty method call -(void)canRotate{}

※Addition, you may want to force the previous screen to get back to portrait when returned from current screen

//①. Add below method to App Delegate

/*
 全部の画面は縦(Project->Target->General->Portrait checkbox)で表示されますが、canRotate関数を持っているクラスは横に回転できます。
*/
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    UIViewController* topVC = [self topViewControllerWith: self.window.rootViewController];
    if ([topVC respondsToSelector:@selector(canRotate)]) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    return UIInterfaceOrientationMaskPortrait;
}

/* get the top ViewController */
- (UIViewController*) topViewControllerWith:(UIViewController *)rootViewController {
    if (rootViewController == nil) { return nil; }
    if ([rootViewController isKindOfClass: [UITabBarController class]]) {
        return [self topViewControllerWith: ((UITabBarController*) rootViewController).selectedViewController];
    }
    else if ([rootViewController isKindOfClass: [UINavigationController class]]) {
        return [self topViewControllerWith: ((UINavigationController*) rootViewController).visibleViewController];
    }
    else if (rootViewController.presentedViewController != nil) {
        return [self topViewControllerWith: [rootViewController presentedViewController]];
    }
    return rootViewController;
}

//② in the screen that will be rotated, add an empty method call -(void)canRotate{}
-(void)canRotate{}

//Addition, you may want to force the previous screen to get back to portrait when returned from current screen
-(void)back{
    self.canvas.hidden=YES;
    [self dismissViewControllerAnimated:NO completion:^(void){
        [UIDevice.currentDevice setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
    }];
}