4

我的应用程序窗口的根视图控制器是一个子类 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 是应用程序支持的方向之一。

4

1 回答 1

14

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

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

返回值

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

于 2012-12-03T19:42:56.517 回答