0

I started a sample project and added the following code to my view controller. After launch the screen will be on landscape. Then I went to my Storyboard, clicked on my only view, -> editor -> embed in -> Navigation Controller. Launch the program again: the screen will be on portrait and won't change to landscape no matter what I try. Why is that? (All orientations are enabled in my plist.)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

@end
Wil Shipley
  • 9,343
  • 35
  • 59
Segev
  • 19,035
  • 12
  • 80
  • 152

1 Answers1

1

In iOS 6, the rule is that once you are in a UINavigationController, the supportedInterfaceOrientations of this view controller (ViewController) are irrelevant. The only things consulted are:

  • The app (Info.plist).

  • The app delegate (application:supportedInterfaceOrientationsForWindow:)

  • The root view controller, which in your case is now the UINavigationController

Now, I do not entirely understand why your UINavigationController is giving you a portrait-only result. But I do know that if you want to control what the UINavigationController does about rotation, you will have to subclass UINavigationController, make this navigation controller be an instance of your subclass, and put supportedInterfaceOrientations code into your subclass.

Note I tried doing what you said you did, and on my machine, the navigation controller does perform compensatory rotation to all three standard orientations. So I don't understand why you say it is only portrait. However, the answer is still the same.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You are right. After subclassing it I did managed to rotate to landscape but if I'll add `- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; }` in `ViewController` A this `ViewController` will be in portrait (while other view controllers will start in landscape) so saying "supportedInterfaceOrientations of this view controller (ViewController) are irrelevant" is half correct. Am I wrong? – Segev Apr 30 '13 at 18:40
  • `preferredInterfaceOrientationForPresentation` is irrelevant. This is not a "presentation" situation. – matt Apr 30 '13 at 18:42
  • Regarding your note. I was talking about the orientation the view startsup with (without rotating the device). I still not fully working on my end but I'll start another question for that. Thanks. – Segev Apr 30 '13 at 19:00
  • The orientation you start up with is the *first* orientation in the plist. – matt Apr 30 '13 at 19:04
  • How can I force one ViewController to start with landscape and another viewcontroller to start with portrait? They all act like the subclassed NavigationController – Segev Apr 30 '13 at 19:10
  • Yup that's how iOS 6 works. The navigation controller is in charge. You can't force rotation. The only way to force rotation is to use a presented view controller. See my answer here: http://stackoverflow.com/a/15301322/341994 – matt Apr 30 '13 at 20:37
  • I'd love if you'll take a look at my solution here: http://stackoverflow.com/a/16316907/1578927 I'm forcing rotation without using a presented view. – Segev May 01 '13 at 11:05