Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nativescript Angular Detect Swipe to Back for IOS

In Nativescript Angular, I can swipe from left edge to right to go back on iOS. Is there any way i can detect or capture this swipe back?

The reason for this is because I have a button on screen that execute "this.routerExtensions.back()" when tapped. I would like to identify whether a "Back action" is from this button or iOS swipe event. Thanks

like image 428
scholarwithfire Avatar asked Feb 05 '26 19:02

scholarwithfire


1 Answers

This is how I was able to solve it for Android and iOS:

  1. adjust constructor to manipulate the pullToLeft function from Android, and disable the default swipe back from iOS:
constructor(...) {
    if (isAndroid) {
      Application.android.on(
        AndroidApplication.activityBackPressedEvent,
        (data: AndroidActivityBackPressedEventData) => {
          data.cancel = true;
          ...yourCustomFunction()
        }
      );
    } else {
      page.enableSwipeBackNavigation = false;
    }
  }
  1. for Android it is already working - for iOS you need then to write a function you can use on a template (this is also how you can recognize the swipe action):
public onSwipe = (args: SwipeGestureEventData) => {
    if (!isAndroid) {
      if (args.direction === SwipeDirection.right) {
        ...yourCustomFunction()
      }
    }
  };
  1. disable the default PopGestureSerializer and add the swipe function on top of your template/element and it should work on iOS as well:
<StackLayout [interactivePopGestureRecognizer]="false" (swipe)='onSwipe($event)'>
...
</StackLayout>
like image 134
Maxeezy Avatar answered Feb 07 '26 10:02

Maxeezy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!