31

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

//iOS6

-(BOOL)shouldAutorotate
{
    return NO;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

错误 :

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

4

4 回答 4

57

您的代码应如下所示:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

此外,请确保在您Info.plist的应用程序中设置了正确的方向,因为您返回的方向与该方向supportedInterfaceOrientations相交,Info.plist如果它找不到一个共同的方向,那么您将收到该错误。

于 2012-10-02T13:59:33.367 回答
13

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

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

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

信息列表

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
于 2012-11-23T15:53:04.977 回答
9

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

于 2012-10-02T13:53:53.840 回答
1

从文档中:

-(NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
}

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

于 2013-02-26T18:16:59.023 回答