Xamarin iOS 中单页强制横向模式? [英] Force Landscape mode in single page in Xamarin iOS?

查看:61
本文介绍了Xamarin iOS 中单页强制横向模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

公共类 OrientationService : IOrientationService{公共无效景观(){((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;}公共无效人像(){((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;}}

它运行良好并符合要求:强制横向模式,即使手中的设备方向是纵向,我需要为 iOS 实现相同的目标,尝试过这个(也尝试了注释代码):

公共类 OrientationService : IOrientationService{公共无效景观(){UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));//((AppDelegate)UIApplication.SharedApplication.Delegate).CurrentOrientation = UIInterfaceOrientationMask.Landscape;//UIApplication.SharedApplication.SetStatusBarOrientation(UIInterfaceOrientation.LandscapeLeft, false);}公共无效人像(){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 版本

解决方案

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

<块引用>

在 AppDelegate.cs 中

public bool allowRotation;

并重写方法

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow){如果(允许旋转==真){返回 UIInterfaceOrientationMask.Landscape;}别的{返回 UIInterfaceOrientationMask.Portrait;}}

<块引用>

依赖服务

公共类 OrientationService : IOrientationService{公共无效景观(){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);}公共无效人像(){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);}}

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

解决方案

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

这篇关于Xamarin iOS 中单页强制横向模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆