[英]mvvmcross ios lock portrait orientation for specific MvxViewController
我在Xamarin mvvmcross中具有支持IOS纵向和横向屏幕方向的解决方案。 IOS 8.0我想添加新的MvxViewController,但只能将其锁定为纵向模式。 我在Internet示例中搜索了ios swift,xamarin表单,但所有这些都不适用于mvvmcross解决方案。有什么方法可以仅使用Xamarin mvvmcroos锁定一个屏幕的纵向模式?
谢谢
这和普通的ViewController没有什么不同。
public override bool ShouldAutorotate() => false;
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() =>
UIInterfaceOrientationMask.Portrait;
实际上对我来说,这种方法正在工作:在App AppDelegate中:我创建的MvxApplicationDelegate类
public bool RestrictRotation = true;
并添加方法:
[Export("application:supportedInterfaceOrientationsForWindow:")]
public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr forWindow)
{
if (this.RestrictRotation)
return UIInterfaceOrientationMask.All;
else
return UIInterfaceOrientationMask.Portrait;
}
然后,对于只需要纵向模式的视图,我使用
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate;
app.RestrictRotation = false;
}
为了支持所有屏幕旋转,我正在设置:
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate;
app.RestrictRotation = true;
}
希望对您有所帮助! 谢谢
我需要一个全是纵向的应用程序,除了某些模态视图是横向的,所以我在MvvmCross 5.0上针对iOS 10做到了这一点:
首先,在Info.plist中设置您的应用所需的方向。
然后,您需要找到[MvxRootPresentation]
,也就是您的根视图。
根视图是将接收来自ShouldAutorotate
& GetSupportedInterfaceOrientations
的调用的ShouldAutorotate
,然后将它们向下传递给可见控制器(如果它们实现了这两种方法都是虚拟的),因此,如果未实现,则默认值为ShouldRotate = false GetSupportedInterfaceOrientations = AllButUpsideDown
在根视图中添加ShouldAutorotate
和GetSupportedInterfaceOrientations
方法,如下所示:
public override bool ShouldAutorotate()
{
return true; // Allows all view to auto rotate if they are wrong orientation
}
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
{
var orientation = UIInterfaceOrientationMask.Portrait;
if (VisibleUIViewController != null)
{
var VisibleMvxViewController = VisibleUIViewController as MvxViewController;
if (VisibleMvxViewController != null)
{
// Check if the view is a Modal View by checking for the MvxModalPresentationAttribute
var attribute = VisibleMvxViewController.GetType().GetCustomAttributes(typeof(MvxModalPresentationAttribute), true).FirstOrDefault() as MvxModalPresentationAttribute;
if (attribute != null)
{
// Visible view is a modal
if (!VisibleUIViewController.IsBeingDismissed)
{
// view is not being dismissed so use the orientation of the modal
orientation = VisibleUIViewController.GetSupportedInterfaceOrientations();
}
}
}
}
return orientation;
}
然后在模态视图(在我的案例中具有MvxModalPresentation属性)上,您想要更改方向,请重写此方法:
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
{
return UIInterfaceOrientationMask.AllButUpsideDown;
}
此解决方案意味着您无需向AppDelegate添加任何内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.