2013-02-21 20 views
0

我有一個使用6.1 SDK的Monotouch 6.0.10 iPhone應用程序,但定位到iOS 4.0及更高版本,我試圖強制其中一個視圖以縱向,使用ShouldAutorotateToInterfaceOrientation。我意識到現在已經棄用,但仍然需要支持iOS4/iOS5設備。ShouldAutorotateToInterfaceOrientation在iOS6設備上被忽略,使用標籤欄控制器

要嘗試隔離問題,我寫了一個最小測試應用程序。它是無XIB的,並且有一個帶有一個選項卡的UITabBarController。該選項卡有一個UINavigationController,而UINavigationController有一個UIViewController(帶有一個hello world按鈕來點擊)。

在AppDelegate中我有:

tabController = new TabController(); 
window.RootViewController = tabController; 

在的UITabBarController,並在UINavigationController的我:

public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation) 
    { 
     return true; 
    } 

在的UIViewController我有:

public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation) 
{ 
    if (toInterfaceOrientation == UIInterfaceOrientation.Portrait) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

好了,上至少iOS 6.1設備,那些ShouldAutorotateToInterfaceOrientation似乎是完全的忽略。那裏的斷點沒有達到,如果我強迫它們在任何情況下都返回錯誤,旋轉仍然發生。

我的理解是ShouldAutomaticallyForwardRotationMethods默認爲true,因此似乎沒有提供解決方案。已經梳理,沒有運氣的論壇,除了格倫·施密特這裏建議:iOS 6 rotations: supportedInterfaceOrientations doesn´t work?但不幸的是,我失去了對如何翻譯,爲的MonoTouch:

QUOTE

If you want to replicate the pre-iOS 6 behaviour where all the views in the navigation stack/tab bar have to agree on an allowable set of orientations, put this in your subclass of UITabBarController or UINavigationController: 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    NSUInteger orientations = [super supportedInterfaceOrientations]; 

    for (UIViewController *controller in self.viewControllers) 
     orientations = orientations & [controller supportedInterfaceOrientations]; 

    return orientations; 
} 

所享有

我明白此外,我甚至不能希望通過ShouldAutoRotate/SupportedInterfaceOrientations來爲我的iOS6用戶解決這個問題,因爲這會導致iOS4/IOS5旋轉失敗。

任何建議非常感謝!

Bill。

回答

2

找到一個解決方案,經過相當多的顛簸後。有人可能能夠改善這一點。

問題

我的應用程序,基於SDK 6.1,目標的iOS4,iOS5的和iOS6的設備。它需要允許所有屏幕旋轉,除了必須以縱向方向固定的屏幕。該應用程序有一個UITabBarController,大多數標籤都有一個UINavigationController,下面有堆疊視圖。

SOLUTION

對於iOS4的/ iOS5的用戶的,關鍵是在應用程序的根視圖控制器(在我的情況下標籤欄控制器)已廢棄的ShouldAutorotateToInterfaceOrientation()方法。此方法不會在任何其他視圖控制器中自動調用,但當然,根視圖控制器可以在當前視圖控制器中調用同名的方法。

對於iOS6用戶,關鍵是應用的根視圖控制器中的ShouldAutorotate()/ GetSupportedInterfaceOrientations()。同樣,這些方法不會在任何其他視圖控制器中自動調用,但根視圖控制器可以在當前視圖控制器中調用同名的方法。

對於在原崗位描述我的簡單測試程序:

在AppDelegate中/ FinishedLaunching:

window.RootViewController = tabController; 

在AppDelegate中:

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations (UIApplication application, UIWindow forWindow) 
{ 
    return UIInterfaceOrientationMask.All; 
} 

在TabController:

public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation) // iOS4/iOS5 only 
{ 
    try 
    { 
     UINavigationController navController = (UINavigationController)SelectedViewController; 
     UIViewController targetController = navController.ViewControllers[0]; 
     if (targetController.Title == "Greetings") 
     { 
      if (toInterfaceOrientation == UIInterfaceOrientation.Portrait) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
    } 
    catch 
    { 
     return true; 
    } 
    return true; 
} 

public override bool ShouldAutorotate() // iOS6+ only 
{ 
    return true; 
} 

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() // iOS6+ only 
{ 
    try 
    { 
     UINavigationController navController = (UINavigationController)SelectedViewController; 
     UIViewController targetController = navController.ViewControllers[0]; 
     return targetController.GetSupportedInterfaceOrientations(); 
    } 
    catch 
    { 
     return UIInterfaceOrientationMask.All; 
    } 
    return UIInterfaceOrientationMask.All; 
} 

在需要變爲縱向視圖控制器:

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() // iOS6+ only 
{ 
    return UIInterfaceOrientationMask.Portrait; 
} 

我的實際應用需要一些稍微複雜,但沿着相同的路線。例如在tabbarcontroller中:

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() 
{ 
    try 
    { 
     UINavigationController navController = (UINavigationController)SelectedViewController; 
     if (navController.Title == "Tracking")  // Are we on the Tracking tab? 
     { 
      // Yes. Looking for RedLaser's BarcodePickerController 
      UIViewController targetController = navController.ViewControllers[1];  // BarcodePicker would be second on nav stack 
      string controllerType = targetController.GetType().Name; 

      if (controllerType == "BarcodePickerController")       // Is this BarcodePicker? 
      { 
       return UIInterfaceOrientationMask.Portrait;        // Yes, force portrait orientation       
      } 
     } 
     else 
     { 
      return UIInterfaceOrientationMask.All;          // No, allow any orientation 
     } 
    } 
    catch 
    { 
     return UIInterfaceOrientationMask.All;           // Not BarcodePicker, allow any orientation 
    } 
    return UIInterfaceOrientationMask.All; 
}