2013-02-13 61 views
0

如何鎖定設備上的更改方向?如何鎖定設備上的方向改變?

我試圖用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return UIInterfaceOrientationLandscapeRight; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight]; 
    [super viewDidAppear:animated]; 
} 

但沒有工作時,我使用的設備。當應用程序在模擬器上運行時,效果很好。

模擬器有iOS 6和設備5.1.1。我做錯了什麼?

+0

工作UIInterfaceOrientationLandscapeRight不是BOOL,它只是UIInterfaceOrientation枚舉的成員。所以你應該會得到一個編譯器錯誤。你想要做的就是回到UIInterfaceOrientation == UIInterfaceOrientationLandscapeRight,如果傳遞的interfaceOrientation是測試你需要與否什麼條件的結果。 – 2013-02-13 10:31:49

回答

3

試試這個:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

該方法返回YES或NO回答這個問題: 「我應該旋轉到這個方向:interfaceOrientation」。 所以你應該返回YES或NO - UIInterfaceOrientationLandscapeRight不是BOOL。 但是,您可以使用比較來返回如上所述的BOOL。

+0

這將適用於iOS版本5及以下,但不適用於iOS6。 – Till 2013-02-13 10:31:26

+0

它的工作原理。但是,當應用程序開始定位是縱向時,如果我將旋轉設備,它會更改並鎖定。但我想我的方向將在開始時展開,不會旋轉 – Arthur 2013-02-13 10:34:10

0

打開您的項目,選擇項目文件,轉到摘要部分,並在「支持的界面方向」小節中設置所需的界面方向。祝你好運!(適用於iOS 6.x的)

0

在項目的.plist文件添加支持的接口方向

,如果你只想做的項目設置爲橫向(右home鍵)

0

1個視圖控制器,你可以做,要麼通過

- (NSUInteger)supportedInterfaceOrientations{ 
return UIInterfaceOrientationMaskPortrait; 
} 

//deprecated 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (BOOL)shouldAutorotate { 
return self.interfaceOrientation == UIInterfaceOrientationPortrait; 
} 

如果你想設置爲整個應用程序,請確保你改變你的Info.plist文件,並添加Supported Interface Orientations

0

您只需添加下列行.m文件

#define IOS_OLDER_THAN_6 ([ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0) 
#define IOS_NEWER_OR_EQUAL_TO_6 ([ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0) 

然後您必須在.m文件中添加以下方法文件

#ifdef IOS_OLDER_THAN_6 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 
#endif 

#ifdef IOS_NEWER_OR_EQUAL_TO_6 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscapeRight; 
} 
#endif 
+0

Ifdefs發生在編譯時,所以此代碼在語法上無效。但是,簡單地重寫這兩種方法都可以正常工作 - iOS 5將忽略'-supportedInterfaceOrientations',iOS 6將忽略'-shouldAutorotateToInterfaceOrientation:'。 – 2013-02-13 10:49:51

+0

我在我的項目中使用了這個工作正常 – thavasidurai 2013-02-13 10:51:20

+0

再看一遍,你說得對,它「有效」 - 但不是你期望的。它的工作原理是因爲'IOS_OLDER_THAN_6'和'IOS_NEWER_OR_EQUAL_TO_6'實際上都是由你的代碼來定義的,所以這兩種方法都被編譯到你的代碼中。如果你刪除了'#ifdef'和'#endif'這兩行,它將不會起任何作用。 – 2013-02-13 10:53:46

0

對於iOS 6支持包括這兩種方法

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

也請注意,這將在Xcode的4.5 +