【问题标题】:Partial safe area on Xamarin.FormsXamarin.Forms 上的部分安全区域
【发布时间】:2020-06-13 15:27:41
【问题描述】:

我有一个包含需要安全区域填充的 ListView 的页面,以便 iPhone X 凸块不会在横向方向上剪切 ListView 内容。但是,ListView 的底部不需要填充;屏幕的圆底角不会干扰。

页面通过从构造函数调用这个来启用安全区域填充:

Xamarin.Forms.PlatformConfiguration.iOSSpecific.Page.SetUseSafeArea(On<Xamarin.Forms.PlatformConfiguration.iOS>(), true);

如何为除底部之外的所有侧面保留安全区域填充?即使用户在使用页面时旋转屏幕方向,该解决方案也必须有效。

【问题讨论】:

    标签: ios xamarin.forms safearealayoutguide


    【解决方案1】:

    可以通过使用Xamarin.Forms.PlatformConfiguration.iOSSpecific 命名空间中的Page.SafeAreaInsets 方法检索其Thickness 值来自定义安全区域。然后可以根据需要进行修改并重新分配给页面构造函数中的 Padding 属性或OnAppearing override:

    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        var safeInsets = On<iOS>().SafeAreaInsets();
        safeInsets.Bottom = 0;
        safeInsets.Top = 35;
        safeInsets.Left = 35;
        safeInsets.Right = 35;
        Padding = safeInsets;
    }
    

    人像的效果:

    风景的效果:

    如果需要在设备方向改变时自定义SafeAreaInsets。有一个OnSizeAllocated的方法可以很方便的使用。

    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);
        if (width != this.width || height != this.height)
        {
            this.width = width;
            this.height = height;
            if (width > height)
            {
                Console.WriteLine("Landscape");
                var safeInsets = On<iOS>().SafeAreaInsets();
                safeInsets.Bottom = 0;
                safeInsets.Top = 0;
                safeInsets.Left = 35;
                safeInsets.Right = 35;
                Padding = safeInsets;
            }
            else
            {
                Console.WriteLine("Portrait");
                var safeInsets = On<iOS>().SafeAreaInsets();
                safeInsets.Bottom = 0;
                safeInsets.Top = 35;
                safeInsets.Left = 0;
                safeInsets.Right = 0;
                Padding = safeInsets;
            }
        }
    }
    

    ==================================更新============= ====================

    如果需要适配越来越多的设备,可以通过一些手机型号判断(比如iPhone X)来设置两侧安全区域的大小。另外,还可以查看是否包含Navigation Bar或 Tab Bar 修改 Thickness 的大小。应该是

    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);
        if (width != this.width || height != this.height)
        {
            this.width = width;
            this.height = height;
            if (width > height)
            {
                Console.WriteLine("Landscape");
                var safeInsets = On<iOS>().SafeAreaInsets();
                // whether is iPhone X
                if (deveiceType.Equals("iPhone X"))
                {
                    //whether contain Navigation Bar
                    if (ContainNavigationBar) { safeInsets.Top = 44; } else { safeInsets.Top = 0; }
                    //whether conatin Tab Bar
                    if (ContainTabBar) { safeInsets.Bottom = 34; } else { safeInsets.Bottom = 0; }
                }
                safeInsets.Left = 35;
                safeInsets.Right = 35;
                Padding = safeInsets;
                Padding = safeInsets;
            }
            else
            {
                Console.WriteLine("Portrait");
                var safeInsets = On<iOS>().SafeAreaInsets();
                // whether is iPhone X
                if (deveiceType.Equals("iPhone X"))
                {
                    //whether contain Navigation Bar
                    if (ContainNavigationBar) { safeInsets.Top = 84; } else { safeInsets.Top = 44; }
                    //whether conatin Tab Bar
                    if (ContainTabBar) { safeInsets.Bottom = 34; }else{ safeInsets.Bottom = 0; }
                }
                safeInsets.Left = 0;
                safeInsets.Right = 0;
                Padding = safeInsets;
            }
        }
    }
    

    只要苹果以后不改变状态栏的高度,我认为硬编码设置的高度不会有问题。

    【讨论】:

    • 代码示例显示设置厚度结构的所有 4 个边。这不会破坏调用 SafeAreaInsets 的目的吗?同样,将插图大小硬编码为 35,这适用于 iPhone X,但不一定适用于未来的手机?
    • @EdwardBrey Okey。第一个问题,它并没有违背调用SafeAreaInserts的目的。因为如果只设置一个边(底部),其他边将不会保留安全区域。因此,我设置了四个边以使其工作。第二个问题,由于Apple不支持为所有设备甚至未来设备设置Thickness的简便方法,我认为您可以通过一些iPhone型号判断(例如iPhone X)来设置其两侧的安全区域大小。我觉得这应该是现在最好的做法,不会影响以后设备的展示太多。我会更新答案来解释它。
    • 看起来您正在尝试保留从未存在的插图。在使用SetUseSafeArea 时,在纵向模式下,通常没有左右插图。横向的顶部和底部插图也是如此。
    • @EdwardBrey 是的,这就是为什么每次都设置四个边的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2018-04-28
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多