如何获得安全区厚度?

时间:2019-11-14 11:25:21

标签: xamarin.forms

我想将图像放置在屏幕顶部。

所以我试图获得最高的安全区插图。

var safeArea=On<Xamarin.Forms.PlateformConfiguaration.iOS>().SafeAreaInsets();
safeArea.Top=-safeArea.Top;
Image.Margin=safeArea;

但所有safeArea属性均为0。

热得要合适的厚度?

1 个答案:

答案 0 :(得分:1)

首先, SafeArea 在iOS 11.0之后可用,因此,在获取iOS版本之前,应先检查其版本。

我建议您可以使用 DependencyService

以表格形式,创建界面

public interface IGetSafeArea
{
  double GetSafeAreaTop();
  double GetSafeAreaBottom();
}

在iOS中

using UIKit;

using xxx;

namespace xxx.iOS
{
    public class GetSafeArea : IGetSafeArea
    {
        public double GetSafeAreaBottom()
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                UIWindow window = UIApplication.SharedApplication.Delegate.GetWindow();              
                var bottomPadding = window.SafeAreaInsets.Bottom;
                return bottomPadding;
            }
            return 0;
        }

        public double GetSafeAreaTop()
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                UIWindow window = UIApplication.SharedApplication.Delegate.GetWindow();
                var topPadding = window.SafeAreaInsets.Top;
                return topPadding;
            }
            return 0;
        }
    }
}

并根据需要调用该方法

var top = DependencyService.Get<IGetSafeArea>().GetSafeAreaTop();