What is the best way to cleanly handle Safe Area declaration for pages?

NickL2035 61 Reputation points
2020-12-11T00:34:47.25+00:00

This link describes a clean way to handle safe area for iOS, however, the code is old and doesn't compile anymore. https://www.xamarinhelp.com/safeareainsets-xamarin-forms-ios/

The advantage of using code like this is that it makes it very convenient to apply it to specific Content Pages.

Can someone offer another solution that works or help rewrite the code in the link?

[assembly: ResolutionGroupName("Enterprise")]
[assembly: ExportEffect(typeof(SafeAreaPaddingEffect), nameof(SafeAreaPaddingEffect))]
namespace Enterprise.iOS.Effects
{
    class SafeAreaPaddingEffect : PlatformEffect
    {
        Thickness _padding;
        protected override void OnAttached()
        {
            if (Element is Layout element)
            {
                if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
                {
                    _padding = element.Padding;
                    var insets = UIApplication.SharedApplication.Windows[0].SafeAreaInsets; // Can't use KeyWindow this early
                    if (insets.Top > 0) // We have a notch
                    {
                        element.Padding = new Thickness(_padding.Left + insets.Left, _padding.Top + insets.Top, _padding.Right + insets.Right, _padding.Bottom);
                        return;
                    }
                }
                // Uses a default Padding of 20. Could use an property to modify if you wanted.
                element.Padding = new Thickness(_padding.Left, _padding.Top + 20, _padding.Right, _padding.Bottom);
            }
        }

        protected override void OnDetached()
        {
            if (Element is Layout element)
            {
                element.Padding = _padding;
            }
        }
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2020-12-11T03:43:11.07+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The code is still working but we have to complete it in shared project .

    Shared project

       //create a class   
        public class SafeAreaPaddingEffect : RoutingEffect  
           {  
               public SafeAreaPaddingEffect() : base("Enterprise.SafeAreaPaddingEffect")  
               {  
               }  
           }  
    
    
       //xaml  
    
       xmlns:effect="clr-namespace:FormsApp"  
    
        <Grid>  
               <Grid.RowDefinitions>  
                   <RowDefinition Height="100"/>  
                   <RowDefinition Height="*" />  
               </Grid.RowDefinitions>  
               <ContentView BackgroundColor="Green">  
                   <ContentView.Effects>  
                      <effect:SafeAreaPaddingEffect />  
                   </ContentView.Effects>  
                   <Label Text="Hello, from XamarinHelp.com" />  
               </ContentView>  
           </Grid>  
    

    Test

    47257-ca2pture.png

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful