Search code examples
iosiphonerotationscreen-rotation

Rotation screen wint navigation controller


Can I disable rotation for all screens neither one for iPhone. And how can I do it? I do a lot of combinations with initial portrate/landscape and follow methods but not get it. If I have enable landscape in app settings all screens are rotates. I can not now use device, I test it on simulator.

 - (BOOL)shouldAutorotate
 {
return NO;
}

- (NSUInteger)supportedInterfaceOrientations
 {
//Choose your available orientation, you can also support more tipe using the symbol |
//e.g. return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)
return (UIInterfaceOrientationMaskPortrait);
}

Solution

  • i ran into the same problem first of all check mark on all orientation in built setting>targets>deployment info. then in your app delegate file implement this:

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    
    UIViewController *currentViewController = [self topViewController];
    
    // Check whether it implements a dummy methods called canRotate
    if ([currentViewController respondsToSelector:@selector(canRotate)]) {
        // Unlock landscape view orientations for this view controller
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    
    // Only allow portrait (standard behaviour)
    return UIInterfaceOrientationMaskPortrait;
    

    }

    -(UIViewController*)topViewController {
    return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    

    }

    - (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController{     
    

    if ([rootViewController isKindOfClass:[UINavigationController class]]) { UINavigationController* aNavigationController = (UINavigationController*)rootViewController;

        return [self topViewControllerWithRootViewController:aNavigationController.visibleViewController];
    
    }
    else
    {
        return rootViewController;
    }
    

    }

    in your view controller(assume E) which you want to be in portrait/landscape..

    add this:

    //run time flag method
    

    -(void)canRotate//also declare in header file {

    }

    - (BOOL)shouldAutorotate
    

    { return YES; }

    you are done.

    for more info you can go to this link