2012-10-17 53 views
4

我使用的Xcode 4.5.1和使用條件我想我appliaction只在橫向的IOS(不論是iOS 5和iOS 6)

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


#ifdef IOS_OLDER_THAN_6 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 
    [image_signature setImage:[self resizeImage:image_signature.image]]; 
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
} 
#endif 
#ifdef IOS_NEWER_OR_EQUAL_TO_6 
-(BOOL)shouldAutorotate { 
    return YES; 
} 
- (NSUInteger)supportedInterfaceOrientations { 
    [image_signature setImage:[self resizeImage:image_signature.image]]; 
    return UIInterfaceOrientationMaskLandscapeLeft; 
} 
#endif 

我加入信息的關鍵。 plist中。 enter image description here

<key>UISupportedInterfaceOrientations</key> 
    <array> 
     <string>UIInterfaceOrientationLandscapeRight</string> 
     <string>UIInterfaceOrientationLandscapeLeft</string> 
    </array> 

,並設置支持接口方向

,如果我有添加鍵值中的info.plist,並設置支持方向,並且沒有使用下面的代碼,應用程序不是在內部監督辦公室工作5.0

此代碼是工作,但我想用小替代概念....請幫助我..

在此先感謝!

+0

您是否嘗試過只設定一個方向在你的info.plist(目標概要)? –

+0

我嘗試了...但沒有解決方案 – Deepesh

+0

這些宏的使用完全不正確...... – brigadir

回答

3

你的方法不健康。只有當#ifdef語句爲真時,預處理器塊纔會被編譯。你所做的是使預處理器語句依賴於運行時參數。

您可以嘗試刪除所有預處理器塊。使您的部署目標爲5.0。如果需要的話,這些方法是回調方法,所以它們將被系統調用。 iOS5將調用shouldAutorotateToInterfaceOrientation方法,iOS6將相應地調用supportedInterfaceOrientations和iOS6的旋轉方法。所以你應該在運行時處理所有系統差異,而不是編譯時,除非你故意爲兩個不同的系統編譯兩個不同版本的應用程序。

+0

我已經設置了部署目標5.0 – Deepesh

+0

謝謝...對於解釋 – Deepesh

0

還有另一種方式 - 無需設置允許方向:

嘗試這種解決方案 - 它適用於iOS 5和iOS 6

UINavigationController Force Rotate

然後,您可以實現shouldRotate ..委託方法,只允許它在風景方向之間旋轉。

祝你好運!

+0

如果我使用singleViewController? – Deepesh

+0

這個解決方案應該在主視圖控制器中(或者首先會出現) - 只是爲了確保它將是橫向的。從此,它不再重要 - 如果旋轉不允許縱向顯示。 –

1

謝謝MR。 basar ......我解決我的問題......我解釋如何解決我的問題....

如果設置在你的應用方向在iOS 5和iOS 6的

然後folllow步驟

1)轉到項目 - >選擇支持接口取向

例子: - 你要選擇橫向左,右景觀。請參見圖像 enter image description here

2)當您選擇取向,然後自動售貨機如果沒有添加,則在info.plist中添加密鑰(檢查info.plist),然後在info中添加密鑰。的plist

實施例: -

<key>UISupportedInterfaceOrientations</key> 
    <array> 
     <string>UIInterfaceOrientationLandscapeRight</string> 
     <string>UIInterfaceOrientationLandscapeLeft</string> 
    </array> 

3)中的每個的ViewController

//對於IOS 5

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 
     [image_signature setImage:[self resizeImage:image_signature.image]]; 
     return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
    } 

//對於IOS 6

-(BOOL)shouldAutorotate { 
    return YES; 
} 
- (NSUInteger)supportedInterfaceOrientations { 
    [image_signature setImage:[self resizeImage:image_signature.image]]; 
    return UIInterfaceOrientationMaskLandscapeLeft; 
} 
添加代碼

注: -

1)不需要條件(IOS版本)...因爲shouldAutorotateToInterfaceOrientation (IOs 5supportedInterfaceOrientations (IOS 6)是代表,它的調用運行時... (請參閱basar答案)。

2)如果你只工作IOS 6,那麼不需要代碼。只有步驟1和步工作2

-2

刪除這些預處理器塊,添加此 ,可以支持的iOS5和iOS6的,我們需要提供回調在iOS6的情況下autorotations ....`[的UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]。

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

,我們需要調用

- (NSUInteger)supportedInterfaceOrientations { 
return UIInterfaceOrientationMaskPortrait; 

}

-(BOOL)shouldAutoRotate{ 
    return YES; 
    } 

對於iOS5的

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
     { 
      return ((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == 
     UIInterfaceOrientationPortraitUpsideDown)); } 
+1

-1失去耐心與以往相同的格式不正確的複製答案.... – kleopatra