preferredInterfaceOrientationForPresentation必须返回支持的接口方向

Pet*_*isu 30 cocoa ios ios6

此错误没有意义,因为UIInterfaceOrientationLandscapeRight支持的方向返回首选方向

//iOS6

-(BOOL)shouldAutorotate
{
    return NO;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}
Run Code Online (Sandbox Code Playgroud)

错误:

由于未捕获的异常'UIApplicationInvalidInterfaceOrientation'而终止应用程序,原因是:'preferredInterfaceOrientationForPresentation必须返回支持的接口方向!'

lms*_*lms 57

您的代码应如下所示:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}
Run Code Online (Sandbox Code Playgroud)

此外,请确保Info.plist您已为自己的app设置了正确的方向,因为您返回的supportedInterfaceOrientations内容与之相交,Info.plist如果找不到常见的方向,那么您将收到该错误.

  • 请注意,它是"UIInterfaceOrientationMaskLandscape"的"Mask"部分,这是此答案的重要部分.原始海报用户在他的方法中使用了错误的枚举.Apple为这种方法创建了一组新的枚举/选项似乎有点愚蠢,这导致人们犯了这个简单的错误 - 另外Xcode甚至不提供任何编译器时间检查,因为该方法返回NSUInteger. (4认同)

coc*_*oco 13

如果shouldAutorotate设置为YES,则仅调用supportedInterfaceOrientations

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}
Run Code Online (Sandbox Code Playgroud)

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

info.plist中

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Run Code Online (Sandbox Code Playgroud)


bor*_*den 9

这些是错误的枚举supportedInterfaceOrientations.你需要使用UIInterfaceOrientationMaskLandscapeLeft等(注意中间的单词掩码)