Force Landscape mode in single page in Xamarin iOS?

别来无恙 提交于 2020-01-02 09:11:32

问题


I use a dependency service to force landscape for a single page in Android and iOS, this is for Android:

public class OrientationService : IOrientationService
    {
        public void Landscape()
        {
            ((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
        }

        public void Portrait()
        {
            ((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
        }
    }

it works well and as required: forcing the landscape mode, even the the device orientation in hand is portrait, I need to achieve the same for iOS, tried this (tried also the commented code):

public class OrientationService : IOrientationService
    {
        public void Landscape()
        {
            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()
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
            //((AppDelegate)UIApplication.SharedApplication.Delegate).CurrentOrientation = UIInterfaceOrientationMask.Portrait;
            //UIApplication.SharedApplication.SetStatusBarOrientation(UIInterfaceOrientation.Portrait, false);
        }
    }

but this only switch to landscape if the device position in landscape mode, not like the Android version


回答1:


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


来源:https://stackoverflow.com/questions/52925584/force-landscape-mode-in-single-page-in-xamarin-ios

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