Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Shell/NavigationPage TitleView #20959

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using RectangleF = CoreGraphics.CGRect;
using SizeF = CoreGraphics.CGSize;
using Microsoft.Maui.Platform;
using Microsoft.Maui.Layouts;

namespace Microsoft.Maui.Controls.Handlers.Compatibility
{
Expand Down Expand Up @@ -1766,6 +1767,9 @@ class Container : UIView
UIImageView _icon;
bool _disposed;

//https://developer.apple.com/documentation/uikit/uiview/2865930-directionallayoutmargins
const int SystemMargin = 16;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you include a comment with a link to the Apple docs or similar?


public Container(View view, UINavigationBar bar) : base(bar.Bounds)
{
if (OperatingSystem.IsIOSVersionAtLeast(11) || OperatingSystem.IsMacCatalystVersionAtLeast(11))
Expand Down Expand Up @@ -1796,6 +1800,44 @@ public Container(View view, UINavigationBar bar) : base(bar.Bounds)
ClipsToBounds = true;
}

// A solution based on this code https://github.dev/devxoul/UINavigationItem-Margin
// responsible for removing the system margin inside the navigation bar
UIEdgeInsets CalculateUIEdgeInsets()
{
var type = UIBarButtonSystemItem.FixedSpace;
var spacer = new UIBarButtonItem(type, (_, _) => { });

if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
{
spacer.Width = SystemMargin + 8;
}
else
{
spacer.Width = SystemMargin - 16;
}

nfloat screenWidth = UIScreen.MainScreen.Bounds.Size.Width;

// A margin of private class `UINavigationButton` is different from custom view
if (!UIDevice.CurrentDevice.CheckSystemVersion(11, 0) && screenWidth < 375)
{
// 3.5 and 4 inch
spacer.Width += 8;
}
else if (screenWidth >= 414)
{
// 5.5 inch
spacer.Width -= 4;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you done tests with 6.1" and 6.7" simulators or devices?

}

return new UIEdgeInsets(0, spacer.Width, 0, spacer.Width);
}

public override UIEdgeInsets AlignmentRectInsets
{
get => CalculateUIEdgeInsets();
}

void OnTitleViewParentSet(object sender, EventArgs e)
{
if (sender is View view)
Expand Down Expand Up @@ -1892,10 +1934,16 @@ public override void LayoutSubviews()
if (_icon != null)
_icon.Frame = new RectangleF(0, 0, IconWidth, Math.Min(toolbarHeight, IconHeight));

if (_child?.VirtualView != null)
if (_child?.VirtualView is IView view)
{
Rect layoutBounds = new Rect(IconWidth, 0, Bounds.Width - IconWidth, height);

if (view.HorizontalLayoutAlignment != Primitives.LayoutAlignment.Fill ||
view.VerticalLayoutAlignment != Primitives.LayoutAlignment.Fill)
{
view.Measure(Bounds.Width, Bounds.Height);
layoutBounds = view.ComputeFrame(new Rect(0, 0, Bounds.Width, Bounds.Height));
}
_child.PlatformArrangeHandler(layoutBounds);
}
else if (_icon != null && Superview != null)
Expand Down
Loading