2012-10-02 67 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; 
} 

注意,正確的方向是 「面膜」! 你試過這個嗎?

相關問題