2017-06-14 147 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; 
} 

注:您将需要重复相同的每一个视图控制器的情况下,你要的方向迫使不止一个。

相关问题