23
23

More than 5 years have passed since last update.

UIViewController: iOS 6 で変更になった画面回転に対応する

Last updated at Posted at 2012-12-26

iOS 6 SDK でビルドしたら自動回転しちゃいけない画面が回転するようになってしまうのは、UIViewController の shouldAutorotateToInterfaceOrientation: が使えなくなったことによる。

各画面(UIViewController)ごとに再実装しなくてはならないのか、と少々ゲンナリしていたが、以下のようなカテゴリによる拡張実装で楽に対応できた。
とりあえずPortrait固定であるなら以下で。

// iOS 6以降で自動回転に対応するためのカテゴリ
@interface UIViewController (UIViewControllerForIOS6)

@end

@implementation UIViewController (UIViewControllerForIOS6)

// iOS 6 以降で有効. iOS 5 以前では実行されない
- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        // iPhone がサポートする向き
        return UIInterfaceOrientationMaskPortrait;
    }

    // iPad がサポートする向き
    return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        // iPhone
        return NO;
    }

    return YES;
}

@end

23
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
23