MarkPW
MarkPW

Reputation: 140

Removing back swipe gesture from page using xamarin forms

Is there a way i can disable the back swipe to previous page option for iOS on one single page of my project ?

Upvotes: 2

Views: 2817

Answers (2)

Gerald Versluis
Gerald Versluis

Reputation: 33993

You can achieve this by implementing a custom renderer and setting the right property for this. You can see a sample implementation underneath. The right property, in this case, is InteractivePopGestureRecognizer which you need to set to false.

Do this in the ViewWillAppear so the NavigationController is initialized.

using DisableSwipe.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ContentPage), typeof(NoBackSwipeRenderer))]
namespace DisableSwipe.iOS
{
    public class NoBackSwipeRenderer : PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            if (ViewController?.NavigationController != null)
                ViewController.NavigationController.InteractivePopGestureRecognizer.Enabled = false;
        }
    }
}

Upvotes: 8

M. Hartner
M. Hartner

Reputation: 440

@Symorp You could do it like so:

public class YourCustomPageRenderer : PageRenderer
{
    private YourCustomPage _yourCustomPage;

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        _yourCustomPage = e.NewElement as YourCustomPage;

        if (_yourCustomPage != null)
        {            
            _yourCustomPage.PropertyChanged += YourCustomPagePropertyChangedEventHandler;
        }
    }

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
        SetInteractivePopGestureRecognizerEnabled(isEnabled: false);
    }

    private void YourCustomPagePropertyChangedEventHandler(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
    {
        if (propertyChangedEventArgs.PropertyName == nameof(YourCustomPage.IsInteractivePopGestureRecognizerEnabled))
        {
            SetInteractivePopGestureRecognizerEnabled(_yourCustomPage.IsInteractivePopGestureRecognizerEnabled);
        }
    }

    private void SetInteractivePopGestureRecognizerEnabled(bool isEnabled)
    {
        var interactivePopGestureRecognizer = ViewController?.NavigationController?.InteractivePopGestureRecognizer;

        if (interactivePopGestureRecognizer != null)
        {
            //Prevents the back-swipe-gesture when the user wants to swipe a page away (from left edge of the screen)
            interactivePopGestureRecognizer.Enabled = isEnabled;
        }
    }
}

public class YourCustomPage : ContentPage
{
    /// <summary>
    /// If you need it as bindable property, feel free to create a <see cref="BindableProperty"/>.
    /// </summary>
    public bool IsInteractivePopGestureRecognizerEnabled { get; set; }
}

Feel free to adjust to your needs! :-)

I omitted the export renderer attribute etc., just for simplicity.

Upvotes: 1

Related Questions