LoginSignup
16
16

More than 5 years have passed since last update.

UIViewController iOS6での画面回転への対応

Posted at

iOS6での画面の回転は、TARGETSのSupported Interface Orientationsでの設定に加えて、各ViewControllerでの設定も必要になります。
SS

UIKitのUINavigationControllerやビューコントローラをモーダルで表示したいときなど、わざわざ回転のためだけに継承するのも煩雑ですので、カテゴリでオーバーライドして対応するのが簡単そうです。

UINavigationController

viewControllersの末尾の要素(現在表示されているビューコントローラ)と同じ画面の回転をサポートするようにする。

UINavigationController+Rotation.m
@implementation UINavigationController (Rotation)

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.viewControllers.lastObject supportedInterfaceOrientations];
}

- (BOOL)shouldAutorotate
{
    return [self.viewControllers.lastObject shouldAutorotate];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}

@end

UIActivityViewController

例はUIActivityViewControllerですが、ビューコントローラをモーダルで表示する場合は以下で対応出来ます。
UIActivityViewControllerをモーダル表示しているビューコントローラと同じ画面の回転をサポートするようにする。

UIActivityViewController+Rotation.m
@implementation UIActivityViewController (Rotation)

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

- (BOOL)shouldAutorotate
{
    return [self.presentingViewController shouldAutorotate];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.presentingViewController preferredInterfaceOrientationForPresentation];
}

補足

class-dumpして確認しましたが、3つのメソッドは

@interface UIViewController : UIResponder <NSCoding, UIAppearanceContainer>

で宣言されており、カテゴリでオーバーライドしても問題なさそうです。(カテゴリでの宣言をカテゴリでオーバーライドしているわけではないので)

16
16
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
16
16