2011-05-16 82 views
20

[[UIDevice currentDevice] orientation]方法返回超出縱向和橫向方向的多個方位。我很清楚檢查返回的方向是否「有效」,不幸的是,如果返回的方向不是「有效」,當我的應用程序需要特定的方向來確定運行哪些方法時,我無法知道哪種方法是適當。確定屏幕的方向而不是設備的方向

我已經嘗試了一些這個解決方案,但現在我唯一已經能夠制定出被檢查,看看如果方向爲LandscapeLeftLandscapeRight,如果它不是我想的畫面縱向。不幸的是,當設備朝上並且屏幕處於橫向時,這不是真的。

我試圖使用父視圖控制器的方向:

parentController.interfaceOrientation; 

不幸的是,它返回UIDeviceOrientationUnknown。我搜索和谷歌搜索誰遇到了這個問題的其他人。對我而言,蘋果似乎很愚蠢,因爲他們的方向不會有LandscapeLeft/RightPortraitUp/Down

我真正需要的是應用程序的方向而不是設備,因爲這些有時不一致。有什麼建議?

回答

49

您是否嘗試過使用UIInterfaceOrientationPortrait/UIInterfaceOrientationLandscape?我通常使用這些方法效果還不錯:

if(UIInterfaceOrientationIsPortrait(viewController.interfaceOrientation)) { 
    //do portrait work 
} else if(UIInterfaceOrientationIsLandscape(viewController.interfaceOrientation)){ 
    //do landscape work 
} 

您也可以嘗試UIDeviceOrientationIsValidInterfaceOrientation

這些只會返回應用程序的方向,這可能與設備的方向不一樣。

如果這些不工作,你也可以嘗試:

[[UIApplication sharedApplication] statusBarOrientation]