How to disable the swipe back in iOS using .net maui?

Tumi Tladi 26 Reputation points
2022-12-27T17:13:29.353+00:00

I’ve seen solutions for Xamarin.Forms but in need of .Net Maui

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,027 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,516 Reputation points Microsoft Vendor
    2022-12-28T03:59:12.69+00:00

    Hello @Tumi Tladi ,

    You mean the swipe to go back to previous view gesture in the navigation, right? If so, you can refer to the example below:

    With NavigationPage

    Set a NavigationPage as MainPage in App

    MainPage = new NavigationPage(new MainPage());  
    

    Push to NextPage in MainPage

    Navigation.PushAsync(new NextPage());  
    

    Disable he swipe back gesture in NextPage

      protected override void OnAppearing()  
        {  
            base.OnAppearing();  
    #if IOS  
            UINavigationController vc = (UINavigationController)Platform.GetCurrentUIViewController();//using UIKit, find the UINavigationController  
            vc.InteractivePopGestureRecognizer.Enabled = false;  
    #endif  
        }  
    

    ---Update---

    With Shell
    You can overwrite the CreateShellSectionRenderer method in ShellRenderer, and set InteractivePopGestureRecognizer.Enabled = false; for the ShellSectionRenderer. Because the InteractivePopGestureRecognizer.Delegate has been set in the source code of ShellSectionRenderer, please see the source code here.
    And you can use customrenderer with MAUI, see .NET MAUI control customization with handlers - .NET MAUI | Microsoft Learn and Porting Custom Renderers to Handlers in Wiki and refer to the following code:

    In MauiProgram

              builder.UseMauiApp<App>().ConfigureMauiHandlers((handlers)=>{  
    #if IOS  
                   handlers.AddHandler(typeof(Shell), typeof(CustomShellRenderer));  
    #endif  
            });  
    

    CustomShellRenderer should be under Platform/iOS folder

    public class CustomShellRenderer: ShellRenderer  
        {  
            public CustomShellRenderer()  
            {  
      
            }  
        protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)  
        {  
            return new CustomSectionRenderer(this);  
        }  
    }  
    
    public class CustomSectionRenderer : ShellSectionRenderer  
    {  
        public CustomSectionRenderer(IShellContext context) : base(context)  
        {  
             
        }  
        public override void ViewDidLoad()  
        {  
            base.ViewDidLoad();  
            InteractivePopGestureRecognizer.Enabled = false;  
        }  
    }  
    

    Best Regards,
    Wenyan Zhang


    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.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Tumi Tladi 26 Reputation points
    2022-12-29T10:17:21.757+00:00

    With Shell Navigation system, this is the Crash..

    Specified cast is not valid.

    at iOS_SwipeBack.NextPage.OnAppearing() in /Users/tumitladi/Projects/iOS_SwipeBack/iOS_SwipeBack/NextPage.xaml.cs:line 17
    at Microsoft.Maui.Controls.Page.SendAppearing()
    at Microsoft.Maui.Controls.ShellSection.PresentedPageAppearing()
    at Microsoft.Maui.Controls.ShellSection.<PresentedPageAppearing>g__OnPresentedPageParentSet|94_0(Object sender, EventArgs e)
    at Microsoft.Maui.Controls.Element.OnParentSet()
    at Microsoft.Maui.Controls.NavigableElement.OnParentSet()
    at Microsoft.Maui.Controls.Page.OnParentSet()
    at Microsoft.Maui.Controls.Element.set_Parent(Element value)
    at Microsoft.Maui.Controls.BaseShellItem.AddLogicalChild(Element element)
    at Microsoft.Maui.Controls.ShellSection.AddPage(Page page)
    at Microsoft.Maui.Controls.ShellSection.OnPushAsync(Page page, Boolean animated)
    at Microsoft.Maui.Controls.ShellSection.PushStackOfPages(List1 pages, Nullable1 animate)
    at Microsoft.Maui.Controls.ShellSection.GoToAsync(ShellNavigationRequest request, ShellRouteParameters queryData, IServiceProvider services, Nullable`1 animate, Boolean isRelativePopping)
    at Microsoft.Maui.Controls.ShellNavigationManager.GoToAsync(ShellNavigationParameters shellNavigationParameters, ShellNavigationRequest navigationRequest)
    at iOS_SwipeBack.MainPage.OnCounterClicked(Object sender, EventArgs e) in /Users/tumitladi/Projects/iOS_SwipeBack/iOS_SwipeBack/MainPage.xaml.cs:line 16
    at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle)
    at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
    at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
    at iOS_SwipeBack.Program.Main(String[] args) in /Users/tumitladi/Projects/iOS_SwipeBack/iOS_SwipeBack/Platforms/iOS/Program.cs:line 13