0

I'm working on a project, where has many UIViewController and All are in portrait mode. But, want to activate rotation for a particular UIViewController. I have searched about this previously and find out many answer,but all are not worked for me. My application support iOS6 and iOS7 also.

Also, I have tried with Custom NavigationController as mentioned on this link
How to allow only single UIViewController to rotate in both Landscape and Portrait direction?

Please help me.Thanks.

Community
  • 1
  • 1
Monikanta
  • 307
  • 2
  • 8

3 Answers3

0

If you app only allows Device Orientation Portrait in your project plist, this could work for you.

for the specific viewcontroller.m you want to rotate

add this method:

- (BOOL)canAutoRotate
{
    return YES;
}

then inside your AppDelegate.m

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    UIViewController *currentViewController = [self topViewController];

    if ([currentViewController respondsToSelector:@selector(canAutorotate)]) {
        NSMethodSignature *signature = [currentViewController methodSignatureForSelector:@selector(canAutorotate)];

        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

        [invocation setSelector:@selector(canAutorotate)];
        [invocation setTarget:currentViewController];

        [invocation invoke];

        BOOL canAutorotate = NO;
        [invocation getReturnValue:&canAutorotate];

        if (canAutorotate) {
            return UIInterfaceOrientationMaskAll;
        }
    }

    return UIInterfaceOrientationMaskPortrait;
}

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

- (UIViewController *)topViewControllerWithRootViewController:(UIViewController *)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}
Ted
  • 22,696
  • 11
  • 95
  • 109
0

First make sure you have enabled all the orientations in your project's deployment info.

Add the below code to your AppDelegate,

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

    // Get topmost/visible view controller
    UIViewController *currentViewController = [self topViewController];

    if([currentViewController isKindOfClass:[ViewControllerThatWantsOrientation class]])
        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* nav = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:nav.visibleViewController];
    }
    else if (rootViewController.presentedViewController)
    {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    }
    else
    {
        return rootViewController;
    }
}

After adding the above code in your AppDelegate, remove the methods like shouldAutorotate, supportedInterfaceOrientations from the NavigationController category and from your viewcontrollers(If you have them).

I hope this could this could help you.

iCanCode
  • 1,001
  • 1
  • 13
  • 24
0

In your app delegate .h Add following line of code :

@interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

       BOOL flagOrientationAll;
}

@property (assign) BOOL flagOrientationAll;

Add following method in your app delegate .m file

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    //NSLog(@"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
    if([UICommonUtils isiPad]){
        return UIInterfaceOrientationMaskAll;
    }else if(flagOrientationAll == YES){
        return UIInterfaceOrientationMaskAll;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

Implement following way in your view which you want to rotate in both portrait and landscape both for iPhone device

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.delegate = self;

    PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *) [[UIApplication sharedApplication] delegate];
    delegate.flagOrientationAll = YES;
 }
}

-(void)viewWillDisappear:(BOOL)animated
{
    //NSLog(@"viewWillDisappear -- Start");
     PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *)[[UIApplication sharedApplication] delegate];
        delegate.flagOrientationAll = NO;
}

Check My Post : iOS 7 Interface Orientation

Community
  • 1
  • 1
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45