2012-01-20 115 views
7

我正在爲iOS 5開發一個應用程序,它必須以橫向模式運行。我的問題是,我不能讓它翻轉。強制iOS應用程序在橫向模式下啓動

我曾嘗試加入「初始界面方向」設置爲「風景(右home鍵)」 並添加下面的方法來我的視圖控制器:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

我可以環繞它如何我的頭應該工作。 (我發現代碼here

我也想知道如何使用Xcode 4.2項目設置中提供的「支持的設備方向」,它似乎沒有做任何事情。

我一直在尋找的網站和各地一直沒能找到解決我的問題的例子。

謝謝。

+0

http://stackoverflow.com/a/17628668/468724 –

回答

16

在您的應用程序的Info.plist文件中,添加UIInterfaceOrientation 鍵並將其值設置爲 橫向模式。對於橫向 方位,您可以將此密鑰的值設置爲 UIInterfaceOrientationLandscapeLeftUIInterfaceOrientationLandscapeRight

鋪陳在橫向模式下您的意見,並確保他們的自動尺寸選項設置正確。

覆蓋您的視圖控制器的shouldAutorotateToInterfaceOrientation:方法,並僅爲 所需的橫向方向返回YES,對於縱向方向返回YES 。

+0

謝謝。 我的確如你所描述的那樣做了。我有更多然後一個子視圖,發現我必須覆蓋所有控制器上的方法之前,我得到它的工作。 – bvd

+0

如果您的info.plist你把UIInterfaceOrientationLandscapeRight其他景觀上方,然後你就可以開始你的景觀 –

0

我認爲,如果你在項目支持的接口方向的plist它會工作。只需添加一個名爲「支持的接口方向」的新行,並將其設置爲數組類型,併爲每個想要的景觀視圖添加2個項目。沒有測試過這只是一個快速猜測

+0

我曾嘗試與沒有運氣。 – bvd

1

當你在XCode中點擊你的項目的名稱,並選擇在目標與您的目標,摘要選項卡下有支持的設備方向設置的可視化表示。這就是我設法做到的。

-2

試試這個,這將幫助你,你viewDidLoad中編寫代碼。

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; 
+0

的UIDevice setOrientation應用程序是隻讀的,不能進行設置。 –

+0

只讀屬性沒有setter。 – SyntheticMeshwork

10

使用的appDelegate以下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; 

還設置所需的方位

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

//Here are some iOS6 apis, used to handle orientations. 
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) 
- (NSUInteger)supportedInterfaceOrientations - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
+0

shouldAutorotateToInterfaceOrientation:不叫幫我的讚賞 –

+0

@JigPatel使用一些在iOS6的這些新方法' - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) - (NSUInteger)supportedInterfaceOrientations - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation' – vishy

相關問題