[iOS6] アプリの画面回転の問題について
2014/06/02
iOS6になってから、画面回転の書き方が大幅に変わったようです。
以前は下記のように画面を回転をする場合はreturn YES;で返せばよかったのですが、deprecatedになってしまったようです。
1 2 3 4 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } |
この代わりにiOS6では下記のような書き方をします。(縦横上下回転の場合)
1 2 3 4 5 6 7 8 9 | - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; } |
そして回転した場合の記述を下記のようにしておくと便利でしょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { switch (toInterfaceOrientation) { case UIInterfaceOrientationPortrait: // ホームボタンが下にある場合 break; case UIInterfaceOrientationPortraitUpsideDown: // ホームボタンが上にある場合 break; case UIInterfaceOrientationLandscapeLeft: // 左側面が下になっている場合 break; case UIInterfaceOrientationLandscapeRight: // 右側面が下になっている場合 break; default: break; } } |
しかし、これだけではなく他にも書き換えが必要な部分があります。
AppDelegate.mなどに
1 2 | UINavigationController *ctrl = [[UINavigationController alloc] initWithRootViewController:viewController]; [window addSubView:ctrl.view]; |
と設定している場合、
[window addSubView:ctrl.view];
↓
[window setRootViewController:ctrl];
に変更が必要です。(私はこれですこしハマりました)
iOS5以前のために、shouldAutorotateToInterfaceOrientationは一応残しておいてもいいかもしれませんが、それはアプリの都合に合わせて行うとよいと思います。