user1302553
user1302553

Reputation:

Interface orientation in iOS 6.0

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.

Upvotes: 25

Views: 36894

Answers (5)

uerceg
uerceg

Reputation: 4807

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;
}

Upvotes: 19

user593733
user593733

Reputation: 219

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.

Upvotes: 2

Nicolas Miari
Nicolas Miari

Reputation: 16246

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;
}

Upvotes: 7

James Laurenstin
James Laurenstin

Reputation: 506

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

Upvotes: 11

Deji
Deji

Reputation: 546

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

Upvotes: 1

Related Questions