iOS 6在dismissModalViewController之后恢复方向

时间:2012-09-22 13:03:35

标签: ios ios6

我有一个UITabBarViewController纵向模式,我在景观中推送modalViewController。当我按下模态时,旋转会变为横向,但是当我将其解除时,方向仍保留在横向中而不是恢复为纵向。

代码:

横向模式的CustomUINavigationController

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

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

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

uitabbarcontroller

- (BOOL)shouldAutorotate
{
  if ([self inModal]) {
    UINavigationController *nav = (UINavigationController *)self.modalViewController;
    return [[nav.viewControllers lastObject] shouldAutorotate];
  } else {
    return NO;
  }
}

-(NSUInteger)supportedInterfaceOrientations
{
  if ([self inModal]) {
    UINavigationController *nav = (UINavigationController *)self.modalViewController;
    return [[nav.viewControllers lastObject] supportedInterfaceOrientations];
  } else {
    return UIInterfaceOrientationPortrait;
  }    
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  if ([self inModal]) {
    UINavigationController *nav = (UINavigationController *)self.modalViewController;
    return [[nav.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
  } else {
    return UIInterfaceOrientationPortrait;
  }
}

- (BOOL)inModal
{
  if (self.modalViewController && [self.modalViewController isKindOfClass:[UINavigationController class]]) {
    return YES;
  } else {
    return NO;
  }    
}

如果我将shouldRotate设置为YES,则会收到错误:*由于未捕获的异常'UIApplicationInvalidInterfaceOrientation'而终止应用程序,原因:'支持的方向与应用程序没有共同的方向,并且shouldAutorotate返回YES。即使我在UISupportedDeviceOrientations中设置了两个方向。

1 个答案:

答案 0 :(得分:6)

我认为你的问题是由这一行引起的(你的代码中有两次):

return UIInterfaceOrientationPortrait;

我正在尝试做s.th.与您尝试实现的类似,并在此处提出了我的问题:iOS 6 auto rotation issue - supportedInterfaceOrientations return value not respected

您的代码存在的问题是这两种方法不会返回接口方向,而是返回一个掩码,该掩码在其位中编码允许或首选方向的组合。

试试这个:

return UIInterfaceOrientationMaskPortrait;
据我所知,

UIInterfaceOrientationPortrait被映射为0,因此被解释为一组空接口方向(无)。