ios preferredInterfaceOrientationForPresentation 必须返回受支持的界面方向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12690963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:34:34  来源:igfitidea点击:

preferredInterfaceOrientationForPresentation must return a supported interface orientation

ioscocoaios6

提问by Peter Lapisu

This error doesn't make sense, as the preferred orientation UIInterfaceOrientationLandscapeRightis returned by the supported orientation

这个错误没有意义,因为首选方向UIInterfaceOrientationLandscapeRight是由支持的方向返回的

//iOS6

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

Error :

错误 :

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

由于未捕获的异常“UIApplicationInvalidInterfaceOrientation”而终止应用程序,原因:“preferredInterfaceOrientationForPresentation 必须返回受支持的界面方向!”

回答by lms

Your code should look like this:

您的代码应如下所示:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

Also, make sure in your Info.plistyou have set the correct orientations for you app because what you return from supportedInterfaceOrientationsis intersected with the Info.plistand if it can't find a common one then you'll get that error.

此外,请确保您Info.plist的应用程式,你因为你从回报你已经设置了正确的方向supportedInterfaceOrientations与相交的Info.plist,如果它不能找到一个常见的一种,那么你会得到错误。

回答by coco

supportedInterfaceOrientations is only called, if shouldAutorotate is set to YES

仅当 shouldAutorotate 设置为 YES 时才调用 supportedInterfaceOrientations

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

The easiest approach for me, is only to set the Info.plist

对我来说最简单的方法就是设置 Info.plist

info.plist

信息.plist

If you like to support iOS 5 use this code in your view controllers.

如果您想支持 iOS 5,请在您的视图控制器中使用此代码。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

回答by borrrden

Those are the wrong enums for supportedInterfaceOrientations. You need to use UIInterfaceOrientationMaskLandscapeLeft, etc (note the word maskin the middle)

这些是错误的枚举supportedInterfaceOrientations。您需要使用UIInterfaceOrientationMaskLandscapeLeft等(注意中间的字掩码

回答by Panayotis

from the documentation:

从文档:

-(NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
}

Note that the correct orientation is "Mask"! Did you try this?

请注意,正确的方向是“蒙版”!你试过这个吗?