preferredInterfaceOrientationForPresentation必须返回支持的接口方向(iOS 6)

时间:2012-12-03 18:32:59

标签: ios uiviewcontroller ios6 auto-rotation

我的应用程序窗口的根视图控制器是一个子类UINavigationController。我已将此代码添加到类中:

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

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

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

在我的根UIViewController中,我添加了以下代码:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

当设备在此视图控制器上旋转为横向时,我会显示一个模态视图控制器。当设备旋转回肖像时,我应该关闭模态视图,但是当我这样做时,我得到以下错误:

'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

为什么我收到此错误?


我尝试从根UIViewController中的shouldAutorotate返回YES,现在我收到错误'支持的方向与应用程序没有共同的方向,并且shouldAutorotate返回YES'。这对我来说毫无意义,因为UIInterfaceOrientationPortrait是应用程序支持的方向之一。

1 个答案:

答案 0 :(得分:14)

-supportedInterfaceOrientations中,您需要返回UIInterfaceOrientationMask而不是UIInterfaceOrientation的值。特别是,您似乎想要UIInterfaceOrientationMaskPortrait

以下是-supportedInterfaceOrientations关于返回值的文档:

  

返回值

     

指定支持哪些方向的位掩码。有关有效的位掩码值,请参阅“UIInterfaceOrientationMask”。此方法返回的值不得为0.