Thursday, September 20, 2012

iOS 6 and Interface Orientation

The first experience I got with iOS 6 is about the multiple interface orientation support. I've not understood this change in iOS.

So, if you app supports all interface orientations and in one of your view controllers you need to support only the portrait mode, add the following method to your controller implementation file:
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

In case you have a navigation controller and your portrait view controller is added to this navigation controller, you need to subclass UINavigationController and add this method to the implementation of this subclass:

- (NSUInteger)supportedInterfaceOrientations
{
    return [[self topViewController] supportedInterfaceOrientations];
}

So if the header file looks so:
#import <UIKit/UIKit.h>

@interface MyNavigationController : UINavigationController

@end

in the MyNavigationController.m file I have:

#import "MyNavigationController.h"

@interface MyNavigationController ()

@end

@implementation MyNavigationController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
}

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

- (NSUInteger)supportedInterfaceOrientations
{
    return [[self topViewController] supportedInterfaceOrientations];
}

@end

Same idea fits for the UITabBarController - make your own class derived from UITabBarController with only one method:


- (NSUInteger)supportedInterfaceOrientations
{
    return [[self selectedViewController] supportedInterfaceOrientations];
}


Use this new class in your app instead of the standard UITabBarController.

This is the way I found. You can propose your solution and I will be glad to test it. Maybe this link will help you to solve the problem:

View Controller Programming Guide for iOS: Supporting Multiple Interface Orientations

Other changes in iOS6:

Apple pushes iOS 6.0 Beta 2 OTA update to developers (Update: Video of spinning gears and changelog)