rakuishi.com

[iOS 6] カテゴリによるクラスの拡張で UINavigationController の回転方向を決定する

iOS 6 になって、UINavigationController や UITabBarController の下にある UIViewController で、画面回転を制御するコード(shouldAutorotate, supportedInterfaceOrientations)を書いても、思い通りに動いてくれません。

この際、UINavigationController などの回転方向を決定するには、カテゴリでクラスの拡張をすると思い通りに動作します。

TabBarController とかでも上手く動作すると思います。

UINavigationController+Portrait.h

#import <UIKit/UIKit.h>

@interface UINavigationController (Portrait)

@end

UINavigationController+Portrait.m

#import "UINavigationController+Portrait.h"

@implementation UINavigationController (Portrait)

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

@end