在Xamarin iOS中在单页中强制使用横向模式?

时间:2018-10-22 08:55:42

标签: ios xamarin xamarin.ios screen-orientation

我使用依赖项服务在Android和iOS中为单个页面强制使用横向模式, 这是针对Android的:

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

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

它可以正常工作并按要求运行:强制使用横向模式,即使手边的设备方向为纵向,我也需要在iOS上实现相同的效果,并尝试了此操作(也尝试了注释代码):

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

但是仅当设备处于横向模式时才会切换到横向,而不是Android版本

1 个答案:

答案 0 :(得分:1)

您应该在iOS中做更多的事情

  

在AppDelegate.cs

public bool allowRotation; 

并重写方法

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

    else
     {
        return UIInterfaceOrientationMask.Portrait;
     }
 } 
  

在依赖服务中

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