I needed to get the Safe Area height so I could manipulate a screenshot taken on a device. Stack Overflow to the rescue once again.
Yes the project I'm working on has not started the migration to Maui.
Xamarin Forms
Setup the interface.
IGetSafeArea
public interface IGetSafeArea
{
    double GetSafeAreaTop();
    double GetSafeAreaBottom();
}
Xamarin.iOS
Implement the interface and register it.
GetSafeArea.cs
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;
        }
    }
AppDelegate.cs
 public partial class AppDelegate : FormsApplicationDelegate
    {
       ...
       DependencyService.Register<GetSafeArea>();
       ...
    }
Using It
 if (Device.RuntimePlatform == Device.iOS)
       {
           var safeAreaTop = DependencyService.Get<IGetSafeArea>().GetSafeAreaTop();
       }
 

 
    
Top comments (0)