Skip to content

Commit

Permalink
Handle Safe Area on iOS. It's an Opt in via Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Clancey committed May 6, 2021
1 parent feec478 commit 3074e07
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Core/src/Core/ISafeAreaView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;
namespace Microsoft.Maui
{
public interface ISafeAreaView
{
bool IgnoreSafeArea { get; }
}
}
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Layout/LayoutHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public override void SetVirtualView(IView view)
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

NativeView.View = view;
NativeView.CrossPlatformMeasure = VirtualView.Measure;
NativeView.CrossPlatformArrange = VirtualView.Arrange;

Expand Down
17 changes: 13 additions & 4 deletions src/Core/src/Platform/iOS/LayoutView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ public override CGSize SizeThatFits(CGSize size)
return base.SizeThatFits(crossPlatformSize.ToCGSize());
}

public IView? View { get; set; }

public override void LayoutSubviews()
{
base.LayoutSubviews();

var width = Frame.Width;
var height = Frame.Height;
var bounds = Frame.ToRectangle();
if(View is ISafeAreaView sav && !sav.IgnoreSafeArea)
{
var safe = SafeAreaInsets;
bounds.X += safe.Left;
bounds.Y += safe.Top;
bounds.Height -= safe.Top + safe.Bottom;
bounds.Width -= safe.Left + safe.Right;
}

CrossPlatformMeasure?.Invoke(width, height);
CrossPlatformArrange?.Invoke(Frame.ToRectangle());
CrossPlatformMeasure?.Invoke(bounds.Width, bounds.Height);
CrossPlatformArrange?.Invoke(bounds);
}

internal Func<double, double, Size>? CrossPlatformMeasure { get; set; }
Expand Down

0 comments on commit 3074e07

Please sign in to comment.