mvvmcross ios锁定特定MvxViewController的纵向方向

时间:2017-03-28 15:20:24

标签: ios xamarin xamarin.ios mvvmcross

我在Xamarin mvvmcross中有解决方案,支持IOS纵向和横向屏幕方向。 IOS 8.0 我想添加新的MvxViewController但仅在Portrait模式下锁定它。 我在互联网上搜索了ios swift,xamarin表格,但所有这些都不适用于mvvmcross解决方案 有没有办法如何只使用Xamarin mvvmcroos锁定纵向模式?

谢谢

3 个答案:

答案 0 :(得分:2)

这与普通的ViewController没什么不同。

public override bool ShouldAutorotate() => false;
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() => 
    UIInterfaceOrientationMask.Portrait;

答案 1 :(得分:0)

其实我正在使用这种方法: 在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;
} 

希望它会有所帮助! 谢谢

答案 2 :(得分:0)

我需要一个全画像的应用程序,除了某些模态视图是横向的,所以我在MvvmCross 5.0上为iOS 10这样做:

首先在Info.plist中设置应用所需的方向。

然后你需要找到你的[MvxRootPresentation],也就是你的根视图。

根视图是接收来自ShouldAutorotate&的呼叫的视图。 GetSupportedInterfaceOrientations然后我将它们传递给可见控制器,如果它们实现方法这两个方法都是虚拟的,那么如果没有实现,则默认为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添加任何内容