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

时间:2012-10-02 13:14:06

标签: ios cocoa ios6

此错误没有意义,因为支持的方向

返回首选方向UIInterfaceOrientationLandscapeRight
//iOS6

-(BOOL)shouldAutorotate
{
    return NO;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

错误:

  

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

4 个答案:

答案 0 :(得分:55)

您的代码应如下所示:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

此外,请确保在Info.plist中为您的应用设置了正确的方向,因为您从supportedInterfaceOrientations返回的内容与Info.plist相交,如果找不到常见的那样你就会得到那个错误。

答案 1 :(得分:13)

仅当shouldAutorotate设置为YES

时,才会调用supportedInterfaceOrientations
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

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

info.plist

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

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

答案 2 :(得分:9)

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

答案 3 :(得分:1)

来自文档:

-(NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
}

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