Hello,
Based on your previous link, you implement OnBackButtonPressed in iOS on the latest Maui.
I can reproduce this issue. Here is a solution, you can create a CustomNavigationPageRenderer
in MauiAppproject/Platforms/iOS
folder, then overrider PopViewController
and get the current page, if the back button pressed, this PopViewController will be executed, get current page and invoke SendBackButtonPressed. If the value is true, do not execute back to the previous page. If false, you can back to the previous page.
using Microsoft.Maui.Controls.Handlers.Compatibility;
using UIKit;
namespace MauiApp2.Platforms.iOS
{
public class CustomNavigationPageRenderer : NavigationRenderer
{
public override UIViewController PopViewController(bool animated)
{
if (Element is NavigationPage navigationPage)
{
var currentPage = navigationPage.Navigation.NavigationStack.LastOrDefault();
if (currentPage != null)
{
// Invoke the back button pressed logic
var handled = currentPage.SendBackButtonPressed();
if (handled)
{
// If the back button press was handled, cancel the default behavior
return null;
}
}
}
// Proceed with the default back navigation
return base.PopViewController(animated);
}
}
}
After that, you can reigster this CustomNavigationPageRenderer
in the MauiProgram.cs
for iOS platform.
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 IOS
handlers.AddHandler(typeof(NavigationPage), typeof(MauiApp2.Platforms.iOS.CustomNavigationPageRenderer));
#endif
});
...
In the end, you can add OnBackButtonPressed
in your detailed page.
protected override bool OnBackButtonPressed()
{
//handle your operation
return true;
}
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.