当 View.OnAppearing 被触发时,SafeAreaInsets 为 0

SafeAreaInsets are 0 when View.OnAppearing gets triggered

我正在做一些手动定位,需要考虑 ios 上的安全区域。

我遇到的问题是,当我在我的 OnAppearing 方法中时,SafeAreaInserts 方法 returns 厚度值为 0。旋转设备后(在 SizeChanged 事件中),情况不再如此。由于在进入视图时不会触发 SizeChanged,因此我找不到设置 SafeAreaInsets 时引发的事件。

var safeInsets = On().SafeAreaInsets();

在我共享的 Xamarin.Forms 项目中 I found that the event for a changed SafeAreaInsert gets triggered "a fraction later" but I don't seem to have this event 可用。

对于 ViewSafeAreaInsetsDidChange 方法,它从 PageRenderer 调用。您可以覆盖默认 iOS PageRenderer 并连接到 ViewSafeAreaInsetsDidChange 覆盖。

    public override void ViewSafeAreaInsetsDidChange()
    {

        var page = (Element as Page);
        if (page != null && Forms.IsiOS11OrNewer)
        {
            var insets = NativeView.SafeAreaInsets;
            if(page.Parent is TabbedPage)
            {
                insets.Bottom = 0;
            }
            page.On<PlatformConfiguration.iOS>().SetSafeAreaInsets(new Thickness(insets.Left, insets.Top, insets.Right, insets.Bottom));
        
        }
        base.ViewSafeAreaInsetsDidChange();
    }

更多详情,您可以查看下面的link。 https://github.com/xamarin/Xamarin.Forms/blob/4d49e5786a4c9cfc6eea00bbaa5b57bc09679186/Xamarin.Forms.Platform.iOS/Renderers/PageRenderer.cs