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

Dar*_*ren 4 uiviewcontroller ios auto-rotation ios6

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

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

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}
Run Code Online (Sandbox Code Playgroud)

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}
Run Code Online (Sandbox Code Playgroud)

当设备在此视图控制器上旋转到横向时,我会呈现模态视图控制器.当设备旋转回肖像时,我应该关闭模态视图,但是当我这样做时,我收到以下错误:

'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'
Run Code Online (Sandbox Code Playgroud)

为什么我收到此错误?


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

And*_*ger 14

-supportedInterfaceOrientations,您需要返回值UIInterfaceOrientationMask,而不是UIInterfaceOrientation.特别是,它看起来像你想要的UIInterfaceOrientationMaskPortrait

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

回报价值

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