Xamarin iOS Prevent rotation for specific viewcontroller

≡放荡痞女 提交于 2019-12-21 06:30:03

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/47015508/xamarin-ios-prevent-rotation-for-specific-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!