LoginSignup
9
9

More than 5 years have passed since last update.

UITabBarControllerの中にUINavigationContorollerを置いている場合の回転制御(iOS6以降)

Posted at

以下のようにすることで、それぞれのViewごとに回転を制御できるようになります。
TabBarControllerとNavigationBarでは直接画面制御の判定をせず、実際に表示されるView側で判定をするという仕組みです。
なお、各メソッドは、

shouldAutorotate
 -> supportedInterfaceOrientations
   -> preferredInterfaceOrientationForPresentation

という順番で呼び出されるため、仮に回転をさせない画面(shouldAutorotate = NO の場合)には、それ以外のメソッドを実装する必要は無いようです。

MyTabController.m
#pragma mark - Rotate

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

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

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

MyNavigationController.m
#pragma mark - Rotate

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

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

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

MyViewController.m
#pragma mark - Rotate

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
9
9
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
9
9