25

How to use the following methods to support interface orientation in iOS 6.0:

shouldAutorotate

supportedInterfaceOrientations

preferredInterfaceOrientationForPresentation

As "shouldAutorotateToInterfaceOrientation" is deprecated in iOS 6.0.

Please provide code snippets to support your answers.

Thanks.

  • 1
    You can see a solution for supporting iOS 6 and iOS 5 rotation here: http://stackoverflow.com/questions/12396545/how-to-deal-with-iphone-5-screen-size/12397738#12397738 – Sverrisson Sep 13 '12 at 13:10
  • 1
    See my solution here:http://stackoverflow.com/questions/12662240/how-to-make-app-fully-working-correctly-for-autorotation-in-ios-6/12662433#12662433 – Carina Oct 04 '12 at 00:19
  • Thanks for asking this! I have been trying to figure this out for a while now, because things just haven't worked the way they should. – Justin Oct 25 '12 at 01:23
  • Btw, if any of answers helped you, you should consider marking it as answer. Best regards. – uerceg Jun 26 '13 at 09:51

5 Answers5

19

Deprecated method in iOS 5:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

Replacement in iOS 6 and equivalent of this deprecated iOS 5 method above:

- (BOOL) shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

Hope this helps.


[edit #1: Added my UIViewController which successfully starts in Portrait mode in XCode 4.5 on iPhone 6.0 Simulator]

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        // Custom initialization
    }

    return self;
}

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

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

[#edit 2: Sample code from landscape only application which supports iOS 5 and iOS 6]

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

- (BOOL)shouldAutorotate {

    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

    return UIInterfaceOrientationLandscapeLeft;
}
uerceg
  • 4,637
  • 6
  • 45
  • 63
  • 1
    Replaced it with this but does not affect the broken orientation. Nothing changed. – Chewie The Chorkie Sep 21 '12 at 19:53
  • same here. ipad application should run landscape but runs portrait now – Joris Weimar Sep 21 '12 at 22:31
  • Check my edit #1. This is the source code of my UIViewController which I made to run only in Portrait mode. You can change UIInterfaceOrientations in order to get desired behavior. Since I wanted my UIViewController to show in Portrait mode, I also selected Portrait as preferred orientation for my UIViewController from Interface Builder. – uerceg Sep 24 '12 at 05:39
  • what I don't understand is this: you use iOS 6 but compile to 4.3. Apps don't rotate correctly on a iOS 6 device unless you insert these iOS 6 versions. I think this is not how it should work. – Duck Oct 25 '12 at 23:35
  • If you can give link where you pasted your code, I'd be helpful to see what's not working. Best regards. – uerceg Jan 21 '13 at 14:45
  • If this still doesn't work then make sure that in your app delegate you have set the root view controller rather than simply added a subview to the window: `window.rootViewController = myViewController;` – nicktmro May 05 '13 at 23:09
11

By the way, your settings on your Xcode project settings now take precedence. Make sure that you set the "Supported interface orientations" array properly in your project's settings. That was the issue for me. Removed the undesired ones and my app worked like it did when I compiled with Xcode 4.4.1

James Webster
  • 31,873
  • 11
  • 70
  • 114
James Laurenstin
  • 506
  • 1
  • 6
  • 13
  • 1
    and don't forget (like I did) that you have to do this for both iPhone and iPad deployment... – Eric D'Souza Mar 22 '13 at 18:47
  • I have in Xcode project settings Portatit and LandScape (left and right), but I realize that there ara parir of orientation that runs: UIDeviceOrientationFaceDown and UIDeviceOrientationFaceUp, do you know why? – Albert Català Nov 18 '13 at 20:12
7

My app has an instance of a custom UINavigationController subclass, that presents several view controllers, all in portrait only, except when playing a video, in which case I want to additionally allow both landscape orientations.

Based on @uerceg 's answer, this is my code.

First, I enabled Portrait, Landscape Left and Landscape right in Xcode -> Target -> Summary.

In the UINavigationController subclass's implementation, I #import'ed <MediaPlayer/MediaPlayer.h>.

Then I implemented these methods:

// Autorotation (iOS <= 5.x)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([self modalViewController] && [[self modalViewController] isKindOfClass:[MPMoviePlayerController class]]) {

        // Playing Video: Anything but 'Portrait (Upside down)' is OK
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    else{
        // NOT Playing Video: Only 'Portrait' is OK
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
}


// Autorotation (iOS >= 6.0)

- (BOOL) shouldAutorotate
{
    return YES;
}


-(NSUInteger)supportedInterfaceOrientations
{
    NSUInteger orientations = UIInterfaceOrientationMaskPortrait;

    if ([self modalViewController] && [[self modalViewController] isKindOfClass:[MPMoviePlayerController class]]) {

        // Playing Video, additionally allow both landscape orientations:

        orientations |= UIInterfaceOrientationMaskLandscapeLeft;
        orientations |= UIInterfaceOrientationMaskLandscapeRight;

    }

    return orientations;
}
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
  • Actually, the first condition in both `if` blocks is redundant (if the modal view controller is nil, the `isKindOfClass` test should return 0, i.e. `NO` ). The current version is more verbose, but perhaps easier to understand? – Nicolas Miari Oct 25 '12 at 03:01
  • This was exactly the problem I was trying to solve but it seems like settings in in Xcode -> Target -> Summary override everything. Even if I return NO in the shouldAutorotate function, it still rotates. Any ideas? – noodl_es Nov 07 '12 at 10:29
  • Thanks. Btw, [self modalViewController] is deprecated in iOS 6. I replaced it with [self presentedViewController] and it works fine. – dchakarov Mar 07 '13 at 14:12
  • neither modalViewController, nor presentedViewController worked for me. used visibleViewController – Dmitry Khryukin Mar 11 '13 at 22:32
2

NicolasMiari's code worked for me. A little different spin I had a UITabBarController that presented UINavigationControllers and I was using StoryBoards. The UITabBarController's subclass's implementation is exactly the same and be patient with the Class selection for the Tab Bar Controller in Story Boards. It isn't immediately available even after building.

user593733
  • 219
  • 3
  • 11
1

https://devforums.apple.com/thread/165384?tstart=0

https://devforums.apple.com/thread/166544?tstart=0

There are a number of examples and suggestions in the above threads relating to supporting interface orientation changes on iOS6, the two threads related to issues with game centre views but should be enough to get you started.

You should also check the iOS6 release notes under UIKit, unfortunately I can't give you a direct link since I'm new.

Avoiding posting code here due to NDA

Hope that helps

Deji
  • 536
  • 5
  • 8