1
votes

I have solution in Xamarin mvvmcross which support for IOS portrait and landscape screen orientations. IOS 8.0 I would like to add new MvxViewController but lock it only on Portrait mode. I searched in Internet examples for ios swift, xamarin forms but all of this are not applied for mvvmcross solution Is there way how I can lock Portrait mode only for one screen with Xamarin mvvmcroos?

Thank you

3

3 Answers

2
votes

This is no different from a normal ViewController.

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

Actually for me is working this method : in App AppDelegate : MvxApplicationDelegate class i created

public bool RestrictRotation = true;

and added method :

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

then for view where I want only Portrait mode, I use

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate;
    app.RestrictRotation = false;
}

and for support all screen rotations I am setting :

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate;
    app.RestrictRotation = true;
} 

Hope it will help! Thank you

0
votes

I was needing an app that is all portrait except certain modal views are landscape, so I did it like this for iOS 10 on MvvmCross 5.0:

Firstly set the orientations your app will need in the Info.plist.

Then you will need to find your [MvxRootPresentation], aka your root view.

The root view is the one that will receive the calls from ShouldAutorotate & GetSupportedInterfaceOrientations I then pass these down to the visible controllers if they implement the methods both these methods are virtual so if not implemented the default is ShouldRotate = false GetSupportedInterfaceOrientations = AllButUpsideDown

Add ShouldAutorotate & GetSupportedInterfaceOrientations methods in the root view like this:

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;
}

Then on modal views (that have the MvxModalPresentation attribute in my case) you want to change the orientation override this method:

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

This solution means you don't need to add any thing to the AppDelegate