2013-04-21 81 views
2

我期待在我的應用程序中有一個視圖具有橫向。當手動旋轉時,我設法讓視圖保持在風景中,但是如果設備已經是人像,則無論其支持的方向如何(使用supportedInterfaceOrientations方法設置),它都會保持縱向。有沒有辦法讓視圖自動旋轉?我曾嘗試過:Force Landscape iOS 6

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; 

但這不起作用。

任何建議將不勝感激!

+0

可能重複的[IOS 6強制設備方向到橫向](http://stackoverflow.com/questions/12640870/ios-6-force-device-orientation-to-landscape) – 2013-04-22 18:26:29

回答

-1

定義中的UIViewController以下爲你唯一的景觀視野:

- (UIInterfaceOrientation) supportedInterfaceOrientations 
{ return UIInterfaceOrientationLandscapeLeft; } // or right or both 

這應該防止您的觀點從不斷出現在畫像。從iOS版的文件:

聲明首選呈現定向 當一個視圖控制器 呈現全屏顯示其內容,有時記特定的方向看時,內容 出現最好的。如果 內容只能以該方向顯示,那麼您只需將 返回爲您的方法中唯一的方向。要做到這一點

+0

我已經這樣做了: '返回UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;'在我的代碼中。對不起,我忘了把它包括在我的問題 – 2013-04-21 15:56:58

+0

這將永遠不會打電話。請檢查。 – ani 2014-03-05 10:39:34

2

一種方式是通過重寫preferredInterfaceOrientationForPresentation但是爲了要做到這被稱爲的viewController必須呈現(如模式),而不是推提到here

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
    NSLog(@" preferred called"); 
    return UIInterfaceOrientationPortrait; 
} 

爲了在UINavigationController中顯示你的viewController,使用:

UINavigationController *presentedNavController = [[UINavigationController alloc] initWithRootViewController:protraitViewController]; 

[self presentViewController:presentedNavController animated:YES completion:nil]; 

要讓UINavigationController尊重你的c urrent viewController的方向首選項使用這個簡單的category而不是子分類。

此外,蘋果的文檔的這part是一個很好的閱讀,以更好地理解iOS的方向處理。

+0

我發現這個問題是類似於一個稍微不同的答案,顯然很多人的工作http://stackoverflow.com/questions/12640870/ios-6-force-device-orientation-to-landscape?rq=1 – 2013-04-22 09:11:10