Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin Forms: ios:Page.UseSafeArea is not working for all the children in CarouselPage

SreeeeSreeee INMember ✭✭✭✭✭

I have a CarouselPage having 4 children. For every pages I have added ios:Page.UseSafeArea, but SafeArea property is working only for first 2 pages and for the remaining 2 pages the safe area feature is not working. My XF version is 4.7.0.1080 (latest one).

I found the same issue on Github, is there any solution for this issue?

Best Answer

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited July 15 Accepted Answer

    Could you please suggest a CustomRenderer for this?

    Try to create a ContentPage renderer and set the safe area insets in native ios. Assign the inset value to the page's Padding.

    Page renderer class.

    [assembly: ExportRenderer(typeof(CustomPage), typeof(CustomPageRenderer))]
    namespace App19F_8.iOS
    {
        public class CustomPageRenderer : PageRenderer
        {
            public CustomPageRenderer()
            {
            }
    
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement != null)
                {
                    var inset = new Thickness();
    
                    Device.StartTimer(new TimeSpan(200), () =>
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            if (UIApplication.SharedApplication.KeyWindow != null)
                            {
                                if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
                                {
                                    var safeAreaInsets = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets;
                                    inset.Top = safeAreaInsets.Top;
                                    inset.Bottom = safeAreaInsets.Bottom;
                                    inset.Left = safeAreaInsets.Left;
                                    inset.Right = safeAreaInsets.Right;
                                }
                            }
    
                            ContentPage page = e.NewElement as ContentPage;
                            page.Padding = inset;
                        });
                        return false;
                    });
                }
            }
        }
    }
    

    Make the child pages of the CarouselPage to inherit from the custom page.

    public partial class Page1 : CustomPage
    {
        public Page1()
        {
            InitializeComponent();
        }
    }
    

    Refer to:
    https://forums.xamarin.com/discussion/175443/carouselpage-with-contentpages-in-itemtemplate-doesnt-support-usesafearea

Answers

  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @Sreeee Till the Issue is resolved, have you tried using CustomRenderer to see if it helps?

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited July 14

    @AnubhavRanjan Resolved or not resolved? o:)

    Could you please suggest a CustomRenderer for this?

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited July 15 Accepted Answer

    Could you please suggest a CustomRenderer for this?

    Try to create a ContentPage renderer and set the safe area insets in native ios. Assign the inset value to the page's Padding.

    Page renderer class.

    [assembly: ExportRenderer(typeof(CustomPage), typeof(CustomPageRenderer))]
    namespace App19F_8.iOS
    {
        public class CustomPageRenderer : PageRenderer
        {
            public CustomPageRenderer()
            {
            }
    
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement != null)
                {
                    var inset = new Thickness();
    
                    Device.StartTimer(new TimeSpan(200), () =>
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            if (UIApplication.SharedApplication.KeyWindow != null)
                            {
                                if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
                                {
                                    var safeAreaInsets = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets;
                                    inset.Top = safeAreaInsets.Top;
                                    inset.Bottom = safeAreaInsets.Bottom;
                                    inset.Left = safeAreaInsets.Left;
                                    inset.Right = safeAreaInsets.Right;
                                }
                            }
    
                            ContentPage page = e.NewElement as ContentPage;
                            page.Padding = inset;
                        });
                        return false;
                    });
                }
            }
        }
    }
    

    Make the child pages of the CarouselPage to inherit from the custom page.

    public partial class Page1 : CustomPage
    {
        public Page1()
        {
            InitializeComponent();
        }
    }
    

    Refer to:
    https://forums.xamarin.com/discussion/175443/carouselpage-with-contentpages-in-itemtemplate-doesnt-support-usesafearea

Sign In or Register to comment.