【问题标题】:Force Landscape mode in single page in Xamarin iOS?在 Xamarin iOS 的单页中强制横向模式?
【发布时间】:2019-03-26 07:21:26
【问题描述】:

我使用依赖服务来强制 Android 和 iOS 中单个页面的横向, 这是安卓版的:

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 版本

【问题讨论】:

    标签: ios xamarin xamarin.ios screen-orientation


    【解决方案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);
        }
    }
    

    【讨论】:

    • 只是一个小错字,我认为allowRotation在Portrait中应该是false
    • 您可以根据需要进行设置。您解决问题了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多