Logo

dev-resources.site

for different kinds of informations.

iOS Tips Xamarin Forms - Get Safe Area Height

Published at
9/13/2023
Categories
xamarin
ios
dotnetmaui
mobile
Author
iainrough
Categories
4 categories in total
xamarin
open
ios
open
dotnetmaui
open
mobile
open
Author
9 person written this
iainrough
open
iOS Tips Xamarin Forms - Get Safe Area Height

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();
}
Enter fullscreen mode Exit fullscreen mode

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;
        }
    }
Enter fullscreen mode Exit fullscreen mode

AppDelegate.cs

 public partial class AppDelegate : FormsApplicationDelegate
    {
       ...
       DependencyService.Register<GetSafeArea>();
       ...
    }
Enter fullscreen mode Exit fullscreen mode

Using It

 if (Device.RuntimePlatform == Device.iOS)
       {
           var safeAreaTop = DependencyService.Get<IGetSafeArea>().GetSafeAreaTop();

       }
Enter fullscreen mode Exit fullscreen mode

References

StackOverFlow

xamarin Article's
30 articles in total
Favicon
Linux vs macOS vs Microsoft Windows: Which is Best for Software Development?
Favicon
𝗚𝗲𝘁 𝗗𝗲𝘃𝗘𝘅𝗽𝗿𝗲𝘀𝘀 .𝗡𝗘𝗧 𝗠𝗔𝗨𝗜 𝗖𝗼𝗻𝘁𝗿𝗼𝗹𝘀 𝗳𝗼𝗿 𝗙𝗿𝗲𝗲 𝗕𝗲𝗳𝗼𝗿𝗲 𝗗𝗲𝗰𝗲𝗺𝗯𝗲𝗿 𝟯𝟭, 𝟮𝟬𝟮𝟰!
Favicon
15 Best Chrome Extensions for Developers: Boost Your Productivity and Workflow
Favicon
Top 10 iOS Automation Testing Tools for 2025 (with Key Features)
Favicon
Modern Startup & Login Screens for .NET MAUI + MVVM
Favicon
UI components for MAUI apps
Favicon
How to check the MySQL version on Windows, so easy that even a five-year-old can learn it
Favicon
Building Multi-Page Applications with Xamarin Forms - Tips and Tricks
Favicon
Mobile Development
Favicon
.NET MAUI BorderLessEntry for all platforms
Favicon
Understanding Cross-Platform Development with Xamarin
Favicon
Navigating Cross-Platform Development: A Comparative Analysis of React Native, Xamarin, Flutter, and Electron
Favicon
Xamarin vs. React Native: Which Cross-Platform App Development Framework to Choose
Favicon
Is Xamarin Dead?
Favicon
Crafting Native Mobile Apps Across Platforms with .NET: Unleashing the Power of Xamarin
Favicon
iOS Tips Xamarin Forms - Get Safe Area Height
Favicon
Exploring the Most Popular Cross Platform App Development Frameworks
Favicon
iOS Tips - This app cannot be installed because its integrity could not be verified
Favicon
Sneak Peek at 2023 Volume 3: Xamarin
Favicon
My journey in mobile development - From C# to Swift
Favicon
بهترین آموزش .NET MAUI فارسی
Favicon
Xamarin/C# app
Favicon
Flutter Vs Xamarin- Which Is The Best Cross-Platform Development Framework?
Favicon
Welcome to the New Era of App Development: Introducing Avalonia v11
Favicon
🔒 Introducing Serial Log and Metro Log: Simplifying Your Logging Experience In .NET MAUI! 📝🚇
Favicon
How to Build Mobile Check Capture App with Xamarin.Forms and Dynamsoft Document SDK
Favicon
Sneak Peek at 2023 Volume 2: Xamarin
Favicon
Incredible App Themes for Xamarin.Forms
Favicon
Learn how to Integrate Push Notifications in .NET MAUI
Favicon
App link c# Xamarin

Featured ones: