Force Landscape mode in single page in Xamarin iOS?

烂漫一生 提交于 2019-12-06 06:05:54

You should do something more in iOS

in AppDelegate.cs

public bool allowRotation; 

And rewrite the method

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
 {
    if(allowRotation==true)
     {
        return UIInterfaceOrientationMask.Landscape;
     }

    else
     {
        return UIInterfaceOrientationMask.Portrait;
     }
 } 

in dependency service

public class OrientationService : IOrientationService
{
    public void Landscape()
    {
        AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
        appDelegate.allowRotation = true;

        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
        //((AppDelegate)UIApplication.SharedApplication.Delegate).CurrentOrientation = UIInterfaceOrientationMask.Landscape;
        //UIApplication.SharedApplication.SetStatusBarOrientation(UIInterfaceOrientation.LandscapeLeft, false);
    }

    public void Portrait()
    {
        AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
        appDelegate.allowRotation = true;

        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
        //((AppDelegate)UIApplication.SharedApplication.Delegate).CurrentOrientation = UIInterfaceOrientationMask.Portrait;
        //UIApplication.SharedApplication.SetStatusBarOrientation(UIInterfaceOrientation.Portrait, false);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!