Skip to content

Instantly share code, notes, and snippets.

@dimonovdd
Created December 16, 2021 08:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimonovdd/0ba7487cb03dd254285b98f05de8fcf2 to your computer and use it in GitHub Desktop.
Save dimonovdd/0ba7487cb03dd254285b98f05de8fcf2 to your computer and use it in GitHub Desktop.
Best practice for SafeArea on iOS (Xamarin, MAUI)
using System.Linq;
using Foundation;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace MyNamespace
{
public class DisplayHelper
{
const string IphoneModelPrefix = "iphone";
const string SimulatorKey = "SIMULATOR_MODEL_IDENTIFIER";
const double NotchSafeArea = 34d;
const double DefaultSafeArea = 20d;
const double HomeIndicatorSafeArea = 24d;
static Thickness? safeArea;
public static Thickness GetSafeArea()
{
safeArea ??= CalculateSafeArea();
return safeArea.Value;
}
static Thickness CalculateSafeArea()
=> GetVersionsWithNotch().Contains(GetIPhoneModel())
? new Thickness(0, NotchSafeArea, 0, HomeIndicatorSafeArea)
: new Thickness(0, DefaultSafeArea, 0, 0);
static string GetIPhoneModel()
{
var model = DeviceInfo.DeviceType == DeviceType.Virtual
? NSProcessInfo.ProcessInfo.Environment[SimulatorKey].ToString()
: DeviceInfo.Model;
return model.ToLowerInvariant().Replace(IphoneModelPrefix, string.Empty);
}
static string[] GetVersionsWithNotch()
=> new[]
{
"10,3", "10,6", "11,2", "11,4", "11,6", "11,8", "12,1", "12,3", "12,5", "13,1", "13,2", "13,3", "13,4",
"14,2", "14,3", "14,4", "14,5"
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment