我正在使用 Xamarin MVVMCross 技术开发一个应用程序,并且想问一个关于 InterfaceOrientation 选项的问题:应用程序的所有视图控制器都支持所有方向,但应该只使用纵向模式。当我在纵向模式下从其他人转到该 ViewController 时,由于 AppDelegate.cs 中的方法,我能够将纵向视图控制器保持在纵向模式:
[Export("application:supportedInterfaceOrientationsForWindow:")]
public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr forWindow)
{
if (this.RestrictRotation)
return UIInterfaceOrientationMask.All;
else
return UIInterfaceOrientationMask.Portrait;
}
但是,当我在设备处于 LandscapeLeft(LandscapeRight) 方向时转到我的纵向视图控制器时,不会调用 AppDelegate.cs 方法中的 GetSupportedInterfaceOrientations 并且该视图控制器以横向模式打开。有没有一种方法可以仅针对 Xamarin IOS 中的一个视图控制器以编程方式将方向从横向更改为纵向模式?
谢谢
在你的助手班;
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句