Hello,
You can do it by ShellRenderer and Conditional compilation.
For android platform, please create ShellRenderer in the Platforms/Android folder.
Then override CreateTrackerForToolbar, remove the left-padding by toolbar.ContentInsetStartWithNavigation = 0; toolbar.SetContentInsetsAbsolute(0, 0);
like following code
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform.Compatibility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MauiApp13.Platforms.Android
{
public class MyCustomShellRenderer: ShellRenderer
{
protected override IShellToolbarTracker CreateTrackerForToolbar(AndroidX.AppCompat.Widget.Toolbar toolbar)
{
toolbar.ContentInsetStartWithNavigation = 0;
toolbar.SetContentInsetsAbsolute(0, 0);
return base.CreateTrackerForToolbar(toolbar);
}
}
}
For iOS, create ShellRenderer in the Platforms/iOS folder. Override CreateNavBarAppearanceTracker
method,
then create MyCreateNavBarAppearanceTracker to set navBar.LayoutMargins = UIEdgeInsets.Zero; // Remove extra margins
navBar.ContentMode = UIViewContentMode.ScaleAspectFill; // Adjust content mode` like following code.
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform.Compatibility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIKit;
namespace MauiApp13.Platforms.iOS
{
swift
public class MyCustomShellRendererForiOS:ShellRenderer
{
protected override IShellNavBarAppearanceTracker CreateNavBarAppearanceTracker()
{
return new MyCreateNavBarAppearanceTracker();
}
}
public class MyCreateNavBarAppearanceTracker : IShellNavBarAppearanceTracker
{
public void Dispose()
{
//throw new NotImplementedException();
}
public void ResetAppearance(UINavigationController controller)
{
// throw new NotImplementedException();
}
public void SetAppearance(UINavigationController controller, ShellAppearance appearance)
{
var navBar= controller.NavigationBar;
navBar.LayoutMargins = UIEdgeInsets.Zero; // Remove extra margins
navBar.ContentMode = UIViewContentMode.ScaleAspectFill; // Adjust content mode
}
public void SetHasShadow(UINavigationController controller, bool hasShadow)
{
}
public void UpdateLayout(UINavigationController controller)
{
}
}
}
In the end, please add two handler in the MauiProgram.cs
, you can use Conditional compilation to wrap different implement of adding handler code.
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
handlers.AddHandler(typeof(AppShell), typeof(MauiApp13.Platforms.Android.MyCustomShellRenderer));
`#elif IOS
handlers.AddHandler(typeof(AppShell), typeof(MauiApp13.Platforms.iOS.MyCustomShellRendererForiOS));
#endif
}); ;
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.