Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

supportedInterfaceOrientations method not working on iOS 6 UIViewController

In iOS 5, my application I used the method to change my orientation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

In iOS 6 I think I'm supposed to do this, but it does nothing! My app is rotated not the way I want it to be.

- (BOOL) shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}
like image 771
Chewie The Chorkie Avatar asked Sep 21 '12 20:09

Chewie The Chorkie


3 Answers

It's how I was adding my viewController.

I replaced this line code:

[window addSubview:viewController.view];

by this line:

[window setRootViewController:viewController];
like image 175
Chewie The Chorkie Avatar answered Nov 20 '22 06:11

Chewie The Chorkie


When the device changes orientation, in iOS 6, the system asks the application which orientation it supports. The application will return a number of orientations it accepts.

How does the application determine it's orientation?

  1. First the application checks it's info.plist orientations (this is very important to determine which orientation to use for launch.
  2. Secondly it asks it's application delegate for it's orientations, this can be specified via the -(NSUInteger)application:supportedInterfaceOrientationsForWindow: method. This effectively overrides the info.plist setting, this implementation is optional. By default iPad apps orientate in all directions and iPhone in all but upside down.
  3. Lastly the application delegate queries it's top most view controller, this can be a UINavigationController or a UIViewController... etc, then it specifies how to be presented and if it wants to autorotate. These UIViewControllers can use shouldAutorotate: and supportedInterfaceOrientations methods to tell the app delegate how to present it.

You must make sure you set the root view controller of your window.

Also if you are presenting any other view controllers in full screen such as a modal view controller, this is the one responsible for determining orientation changes or not.

like image 42
Daniel Avatar answered Nov 20 '22 05:11

Daniel


For me the solution worked, the case was different a bit, because I had a UINavigationController. My case was that I needed all Portrait window except one. I had to enable all landscape and the portrait orientations in targets (otherwise it crashes on the only landscape view).

So in this case:

  1. create a subclass for UINavigationController,
  2. insert the rotationspecific stuff there
  3. and use the subclass instead of UINavigationController.
like image 38
Balázs Csordás Avatar answered Nov 20 '22 06:11

Balázs Csordás