2012-09-15 57 views
12

任何人都可以證實,同時支持的iOS 6和iOS 5有沒有點添加新的iOS 6自轉的方法,因爲蘋果的文檔表明,這些方法完全忽略,如果你是還實施iOS 5方法?同時支持的iOS 6和iOS 5自轉

特別是我講的方法- (NSUInteger)supportedInterfaceOrientations- (BOOL) shouldAutorotate - 這些被忽略,合成由編譯器,如果你還實現- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

回答

8

您需要添加新的回調自轉如果你正在打包的應用程序在新的SDK。但是,只有在iOS 6設備上運行此類應用程序時,纔會收到這些回調。對於在較早的iOS版本上運行的設備,會收到較早的回調。如果你沒有實現新的回調,默認行爲是你的應用程序運行在iPad上的所有方向以及除iPhone上的UpsideDown方向之外的所有方向上。

0

你必須從某個地方設置一個標誌(我相信你的信息。 plist),表明你使用了哪兩個。如果您使用新版本,則無法爲iOS 5構建(或者至少,它不適用於iOS 5)。如果您使用舊的方法,則不會調用新的方法。所以是的,你幾乎必須選擇你想要使用的方法,如果你想支持iOS 5,你不能使用新的方法。

+0

你確定,關於國旗?我在iOS 6發行說明中沒有看到任何提及;他們只是聲明如果實施舊方法,新方法將被忽略。 – johnbakers

+0

看看WWDC會議200,大約26'他們談論這個。另外,也要看看會議236 23'45" 開始。不知道這仍然是保密協議,所以請直接看有新的iOS 6新的回調與新的SDK,並運行在打包應用程序收到支持 – Olaf

+3

這兩個回調iOS 6的設備。對於所有其他情況,早期回調的好評。在許多情況下已測試這一點。 –

1
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationMaskAll; 
} 

-(void)viewWillLayoutSubviews 
{ 
if([self interfaceOrientation] == UIInterfaceOrientationPortrait||[self interfaceOrientation] ==UIInterfaceOrientationPortraitUpsideDown) 
    { 
     if (screenBounds.size.height == 568) 
     { 
      //set the frames for 4"(IOS6) screen here 
     } 
     else 

     { 
     ////set the frames for 3.5"(IOS5/IOS6) screen here 
     } 

} 
     else if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft||[self interfaceOrientation] == UIInterfaceOrientationLandscapeRight) 
     { 
     if (screenBounds.size.height == 568) 
     { 
      //set the frames for 4"(IOS6) screen here 
     } 
     else 

     { 
     ////set the frames for 3.5"(IOS5/IOS6) screen here 
     } 


    } 
//it is for IOS5 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
    return YES; 
    } 

- (void)viewWillLayoutSubviews此方法將調用ios5/ios6。此代碼對於ios6/ios5/ios6與3.5英寸屏幕/ 4英寸ios6屏幕都是有用的。

+0

儘量避開它的工作原理來說都非常完美的iOS 5和6級的設備的「幻數」解決方案。 – dooleyo

+0

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationMaskAll; } 將導致崩潰。因爲返回值應該是一個具體的方向,但不是掩碼 –

1

我覺得最優雅的解決方案是:

- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
    return ((1 << toInterfaceOrientation) & self.supportedInterfaceOrientations) != 0; 
} 

難道我錯過了什麼?

+2

或只是'return((1 << toInterfaceOrientation)&self.supportedInterfaceOrientations)!= 0;' – user102008

+0

@danskeel你的回答如何幫助原始問題:支持* both * iOS 5和6?調用'supportedInterfaceOrientations'會在iOS 5中運行時導致崩潰,因爲它是iOS 6的特性。 '***終止由於未捕獲的異常'NSInvalidArgumentException'的應用程序,原因:' - [MYMainMenuTableViewController supportedInterfaceOrientations]:無法識別的選擇器發送到實例0x68649f0'' –

+0

您是否添加了我提到的兩種方法?我想你忘了把第一個添加到你的類'MYMainMenuTableViewController'中。這不是一個問題,因爲它只是一種方法,我可以自己調用它。 – DanSkeel

8

對於iOS5的旋轉。 檢查您所需的方向,並返回YES

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if ((interfaceOrientation==UIInterfaceOrientationPortrait)||(interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)) { 
     return YES; 
    } 
    else return NO; 
} 

的iOS 6.0支持自轉

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationIsPortrait(UIInterfaceOrientationMaskPortrait|| UIInterfaceOrientationMaskPortraitUpsideDown); 

} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationIsPortrait(UIInterfaceOrientationPortrait|| UIInterfaceOrientationPortraitUpsideDown); 

} 
+0

它爲我工作, – Aziz

+0

在iOS 6代碼中,不應該'supportedInterfaceOrientations'返回'UIInterfaceOrientationMaskPortrait'?而不應該'preferredInterfaceOrientationForPresentation'只是返回'UIInterfaceOrientationPortrait'?上面的iOS 6代碼看起來並不適合我。 – Rob

+0

@Rob:我只是建議方法,請回到你自己的方向你想要什麼。如果你不想UIInterfaceOrientationPortraitUpsideDown,你可以返回UIInterfaceOrientationPortrait。 – NaXir

0

,可以支持的iOS5和iOS6的autorotations我們需要提供回調在iOS6的情況下....

[[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)); 
} 
+0

我不認爲有必要對回調進行硬編碼,如果您使用UIKit並且已正確設置項目設置,它們在iOS中會自動執行。 – johnbakers