5

I have iPhone application that supports only Portrait orientation. I want to add to my project view controller that will support only Landscape orientation? Is it possible? If yes how could I achieve that?

I have tried to crate category file like this:

@implementation UINavigationController (Rotation_IOS7)

-(BOOL)shouldAutorotate
    {

        return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations
    {

      return UIInterfaceOrientationMaskLandscape;

    }

If I do this I get this error: Terminating app due to uncaught exception UIApplicationInvalidInterfaceOrientation, reason: Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES

Neeku
  • 3,646
  • 8
  • 33
  • 43
alexxjk
  • 1,681
  • 5
  • 18
  • 30
  • Yes It is posible you can follow this link http://stackoverflow.com/questions/19827540/locking-portrait-orientation-in-view-ios-7/19830292#19830292 – yen Nov 11 '13 at 11:22
  • http://stackoverflow.com/questions/19852408/ipad-app-interface-orientation-changed-after-launch-of-aplicaiton/19852857#19852857 – yen Nov 11 '13 at 11:24
  • thanks for your answer yen but I'm not sure if it's my case. first of all my app aims only for iOS 7 so I'd prefer not to use iOS 6 hacks. and if I change my navigation controller code this way what about another view controllers in its navigation chain? – alexxjk Nov 11 '13 at 11:34
  • put this code in your controller and use UIInterfaceOrientationMaskLandscape instead of UIInterfaceOrientationMaskPortrait. – yen Nov 11 '13 at 11:39

3 Answers3

4

I've tried this and it works: http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

First, add these code to your AppDelegat class.

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
// Get topmost/visible view controller
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:[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;
  }
}

Then, in your landscape view controller, add this method

- (void)canRotate { }
Hanton
  • 616
  • 1
  • 6
  • 13
1

I have search through numerous topics and finally found a working solution.

In my example, I have two VC's:

A -> VC that is embedded inside Nav. Controller and should only support Portrait view.

B -> VC that is not embedded inside a VC and should support Landscape only.

I would like to go from view A to view B (by pressing a button) and back to view then A with the specific orientations still correct.

I. Create a Category for UINavigationController and write the following in its .m file: (the code will be automatically called)

- (NSUInteger)supportedInterfaceOrientations
{
    NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController         supportedInterfaceOrientations]);

    return [self.topViewController supportedInterfaceOrientations];
}

- (BOOL)shouldAutorotate
{
    return self.topViewController.shouldAutorotate;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{
    // You do not need this method if you are not supporting earlier iOS Versions

    return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

II. Create a modal segue between A and B and after that between another one between B and A.

III. Write down in each of the View Controllers .m files the following:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

OR

- (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }

After adding this code. You will be able to change orientation for the single view B.

Teodor Ciuraru
  • 3,417
  • 1
  • 32
  • 39
0

Edit:

create a category in .h and then implement those methods

use these methods in the view controller where you want to support landscape

@implementation UINavigationController (Rotation_IOS7)

-(BOOL)shouldAutorotate
    {

        return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations
    {

      return UIInterfaceOrientationMaskLandscape;

    }
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
  • both methods are not being called in my case. any ideas? – alexxjk Nov 11 '13 at 11:48
  • sorry my stupid questions suhit, I'm a newbie in iOS development. I tried to create category file and put your code there. shouldAutorate method was being called at application start no matter where I imported category .h file. then I just putted this implementation in my view controller .m file. shouldAutorated wasn't being called at all. what am I doing wrong? – alexxjk Nov 11 '13 at 12:10
  • read http://stackoverflow.com/questions/19827540/locking-portrait-orientation-in-view-ios-7?lq=1 and http://stackoverflow.com/questions/12520030/how-to-force-a-uiviewcontroller-to-portait-orientation-in-ios-6?lq=1 – Suhit Patil Nov 11 '13 at 12:52