我的应用程序窗口的 Root View Controller 是 UINavigationController 的子类。我已将此代码添加到类(class)中:
- (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;
}
当设备在这个 View Controller 上旋转到横向时,我会呈现一个模态视图 Controller 。当设备旋转回纵向时,我应该关闭模态视图,但当我这样做时出现以下错误:
'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'
为什么会出现此错误?
我尝试从根 UIViewController 中的 shouldAutorotate 返回 YES,现在我收到错误“支持的方向与应用程序没有共同的方向,并且 shouldAutorotate 返回 YES”。这对我来说毫无意义,因为 UIInterfaceOrientationPortrait 是应用程序支持的方向之一。
最佳答案
在 -supportedInterfaceOrientations 中,您需要从 UIInterfaceOrientationMask 返回值,而不是 UIInterfaceOrientation。特别是,您似乎想要 UIInterfaceOrientationMaskPortrait
这是 -supportedInterfaceOrientations 的文档中关于返回值的内容:
Return Value
A bit mask specifying which orientations are supported. See “UIInterfaceOrientationMask” for valid bit-mask values. The value returned by this method must not be 0.
关于ios - preferredInterfaceOrientationForPresentation 必须返回支持的界面方向 (iOS 6),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13689474/