Skip to content
17 changes: 15 additions & 2 deletions src/Core/src/Platform/iOS/SafeAreaPadding.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CoreGraphics;
using UIKit;
using System;

namespace Microsoft.Maui.Platform;

Expand Down Expand Up @@ -28,6 +29,18 @@ public CGRect InsetRect(CGRect bounds)

internal static class SafeAreaInsetsExtensions
{
public static SafeAreaPadding ToSafeAreaInsets(this UIEdgeInsets insets) =>
new(insets.Left, insets.Right, insets.Top, insets.Bottom);
public static SafeAreaPadding ToSafeAreaInsets(this UIEdgeInsets insets)
{
// Filters out negligible floating-point values from UIKit that may cause layout issues (e.g., 3.5527136788005009e-15).
const double tolerance = 1e-14;

static double ApplyTolerance(double value) => Math.Abs(value) < tolerance ? 0 : value;

return new(
ApplyTolerance(insets.Left),
ApplyTolerance(insets.Right),
ApplyTolerance(insets.Top),
ApplyTolerance(insets.Bottom)
);
}
}