常時横向きなアプリで、スピーカーの位置を考慮して上下の回転をサポートしようと思ったのだけれど、最初は動いていたのがいつの間にか回転しなくなってたのでちょっと調べました。
基本は以下のコードで上下の回転はサポートできた。UIInterfaceOrientation
の値で必要な向きだけ処理できる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | - (void)viewDidLoad { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; [super viewDidLoad]; if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) { CGAffineTransform transform = self.view.transform; CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x); CGPoint center = CGPointMake( bounds.size.height / 2.0, bounds.size.width / 2.0); self.view.center = center; transform = CGAffineTransformRotate( transform, (M_PI / 2.0)); self.view.transform = transform; } } |
1 2 3 4 5 6 7 | - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)); } |
実はUIViewAnimationTransition
で二つのビューをトグルするようにしたので、RootViewControllerを用意して、二つのViewControllerをぶら下げて、それぞれのViewControllerに上記のコードを書いた。そうしたら回転しなくなっちゃった。まあ回転する部分のコードは実績のあるものなので、問題はなかろうと。
RootViewController側で処理するべきだろうと上記のコードを加えたら、回転はするもののViewが描画されなくなった。ここでようやく理解した。RootViewControllerで回転処理されてさらに子のViewも回転処理されたらのであらぬ方向へ行ってしまったのかなと。子のViewControllerで回転処理をコメントアウトしたら問題なく描画されて回転もできた。
結局shouldAutorotateToInterfaceOrientation
も子のViewControllerでは必要ないみたい。