2012-10-02 62 views
28

此错误是没有意义的,为择优取向UIInterfaceOrientationLandscapeRight由支撑定向preferredInterfaceOrientationForPresentation必须返回一个支持的接口方向

//iOS6 

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationLandscapeRight; 
} 

返回错误:

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

回答

52

您的代码应该是这样的:

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationLandscapeRight; 
} 

此外,确保在您的Info.plist你已经设置了正确的方向进行你的应用程序,因为你从supportedInterfaceOrientations返回与Info.plist相交,如果它找不到一个共同的,那么你会得到该错误。

+0

我发现这让我很伤心!我有一个通用的应用程序共享viewcontroller代码,并使用上述代码测试用户惯用语。 iPad必须只是风景,而且所有酒吧的肖像都需要风景。我无法在 – user7865437

+3

处获得正确的方向请注意,它是“UIInterfaceOrientationMaskLandscape”的“面具”部分,它是此答案的重要部分。原来的海报用户在他的方法中使用了错误的枚举。苹果为这种方法创建了一套新的enum/optionss似乎有点愚蠢,导致人们犯这个简单的错误 - 另外Xcode甚至不提供任何编译器时间检查,因为该方法返回NSUInteger。 –

+1

@lms,我的整个应用程序只支持肖像模式,只有一个视图(需要支持横向)。在Plist中,我只为肖像设置了方向,并且在上面的代码中写入了我想要改变风景方向的位置。但它给UIInterfaceOrientationLandscapeRight或UIInterfaceOrientationLandscapeLeft.But我想在我看来。你能告诉我如何得到它。 –

8

这些是supportedInterfaceOrientations的错误枚举。您需要使用UIInterfaceOrientationMaskLandscapeLeft等(记单词掩盖在中间)

14

supportedInterfaceOrientations只调用,如果shouldAutorotate设置为YES

- (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); 
} 
1

从文档:

-(NSUInteger)supportedInterfaceOrientations { 

    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
} 

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

相关问题