Nedo | Lukas
(Maui) Native swipe back on iOS
I have now found a solution.
The Maui NavigationBar is not required and can be completely hidden.
Step 1: Write a CustomHandler. The file can be created for example in the Platforms/IOS folder with the following content:
namespace YOURNAMESPACE.Platforms.iOS;
public class ContentPageHandler : Microsoft.Maui.Handlers.PageHandler
{
protected override void ConnectHandler(Microsoft.Maui.Platform.ContentView nativeView)
{
base.ConnectHandler(nativeView);
ContentPage.Loaded += OnLoaded;
}
protected override void DisconnectHandler(Microsoft.Maui.Platform.ContentView nativeView)
{
ContentPage.Loaded -= OnLoaded;
base.DisconnectHandler(nativeView);
}
ContentPage ContentPage => VirtualView as ContentPage;
void OnLoaded(object sender, EventArgs e) => ConfigureInteractivePopGesture();
void ConfigureInteractivePopGesture()
{
if (this is IPlatformViewHandler handler && handler.ViewController?.ParentViewController?.NavigationController is UIKit.UINavigationController navControler)
{
navControler.NavigationBarHidden = true;
navControler.InteractivePopGestureRecognizer.Delegate = new GestureRecognizerDelegate();
navControler.InteractivePopGestureRecognizer.Enabled = true;
}
}
}
public class GestureRecognizerDelegate : Foundation.NSObject, UIKit.IUIGestureRecognizerDelegate
{
}
Step 2: Register the handler
Simply register the new handler in MauiProgram.cs like here:
[...]
#if IOS
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(ContentPage), typeof(YOURNAMESPACE.Platforms.iOS.ContentPageHandler));
})
#endif
[...]
2 replies