2017-06-14 157 views
0

我正在開發一個帶Xamarin MVVMCross技術的應用程序,並且想問一個關於InterfaceOrientation選項的問題:應用程序的所有視圖控制器都支持所有方向,但應該只使用縱向模式。 當我去從他人肖像模式視圖控制器,我能夠保持在縱向模式下本人的肖像視圖 - 控制由於方法在我AppDelegate.cs:以編程方式將方向從橫向改爲縱向模式xamarin IOS

[Export("application:supportedInterfaceOrientationsForWindow:")] 
public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr forWindow) 
{ 
    if (this.RestrictRotation) 
     return UIInterfaceOrientationMask.All; 
    else 
     return UIInterfaceOrientationMask.Portrait; 
} 

但是,當我去我的portraitview控制器時,裝置在LandscapeLeft(LandscapeRight)方向中,不調用AppDelegate.cs方法中的GetSupportedInterfaceOrientations,並且該視圖控制器在Landscape模式下打開。 有沒有辦法在Xamarin IOS中以編程方式將方向從橫向更改爲縱向模式?

如果要控制單個視圖控制器的方向不使用AppDelegateGetSupportedInterfaceOrientations方法,而不是用你想控制的一個在的ViewController謝謝

回答

0

在你的助手類中;

public static void LockOrientation(UIInterfaceOrientationMask uIInterfaceOrientationMask, UIInterfaceOrientation uIInterfaceOrientation) 
     { 
      if (UIApplication.SharedApplication.Delegate != null) 
      { 
       CommonUtils.LockOrientation(uIInterfaceOrientationMask); 
       UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)uIInterfaceOrientation), new NSString("orientation")); 
      } 
     } 

在AppDelegate.cs

//Set the orientation for rest of the app 
public UIInterfaceOrientationMask OrientationLock = UIInterfaceOrientationMask.All; 

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

現在只需調用LockOrientation方法,無論你想鎖定/改變方向。

例子:

CommonUtils.LockOrientation(UIInterfaceOrientationMask.Portrait, UIInterfaceOrientation.Portrait); 
+1

感謝Singhal2,它就是我一直在尋找! – Andrei

0

這些行將阻止您實現此方法的ViewController獲取任何其他方向,但是縱向。

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() 
{ 
    return UIInterfaceOrientationMask.Portrait; 
} 

注:您將需要重複相同的每一個視圖控制器的情況下,你要的方向迫使不止一個。

相關問題