MvvmCross: How to force a specific orientation on

2019-02-19 21:50发布

问题:

On my iOS 7 app, I only allow "Portrait" on "Supported Device Orientation" for entire application except I need to allow "Landscape" orientation on a "Video player" view. How can I do it with MvvmCross or MvxViewController? I have tried to set those ShouldAutorotate(), GetSupportedInterfaceOrientations() methods, it doesn't do anything. It keeps locked on Portrait mode for "Video player" view. Do anyone know what is the right way to set orientation on a View?

 public class VideoPlayerView : MvxViewController
 {
    public override bool ShouldAutorotate()
    {
        return true;
    }

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

    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
    {
        return UIInterfaceOrientation.LandscapeLeft;
    }

    public override void ViewDidLoad(bool animated)
    {
        Title = "Video";

        base.ViewDidLoad(animated);
    }    

}

UPDATE: I figured out how to fix this problem.

Step 1) For mvvmcross, you have to set up a new ViewPresenter with inherit from MvxModalNavSupportTouchViewPresenter

Step 2) Create a new Navigation controller inherit from UINavigationController with the following code

            public override bool ShouldAutorotate()
            {
                return TopViewController.ShouldAutorotate();
            }

            public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
            {
                var orientation = TopViewController.GetSupportedInterfaceOrientations();
                return orientation;
            }

Step 3) Inside the new View Presenter you just created on Step 1, override CreateNavigationController method and use the new NavigationController you created on Step 2.

Step 4) On the ViewController you want to change the orientation, you can change it by overriding GetSupportedInterfaceOrientations method. For example, on my VideoPlayerView, I have these code below.

    public override bool ShouldAutorotate()
    {
        return true;
    }

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