Search code examples
xamarin.iosrotationautorotate

Xamarin iOS Prevent rotation for specific viewcontroller


Need to prevent screen rotation on specific view controller
I've tried below-

    public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation)
    {
        return false;
    }


    public override bool ShouldAutorotate()
    {
        return false;
    }


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


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


Nothing worked.

Thanks in Advance!


Solution

  • You can solve this problem with this explanation.

    1) In you AppDelegate add a boolean for example

    public bool disableAllOrientation = false;
    

    2) Modify your UIInterfaceOrientationnMask in the AppDelegate

      public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
            {
                if (disableAllOrientation == true)
                {
                  return UIInterfaceOrientationMask.Portrait;
                }
                return UIInterfaceOrientationMask.All;
            }
    

    3) Call in the controller of the view that you want change orientation

    appDelegate.disableAllOrientation = true;
    

    4) and when the view is close or change you need to put again the boolean in false and put your screen in the original orientation only if you want.

    appDelegate.disableAllOrientation = false;
    

    I hope this solve your problem I have the same problem days ago and this help me.