iOS Question Enable Back Gesture without enabling NavigationBarVisible

German Buchmuller

Member
Licensed User
Longtime User
Hi, is it possible to enable the back gesture swipe, without setting NavControl.NavigationBarVisible=True ? Thanks.
 

b4x-de

Active Member
Licensed User
Longtime User
In B4i, the native back swipe gesture is tightly coupled to the NavigationController and the visible NavigationBar. If you create a full-screen app (NavigationBarVisible = False, HideBackButton = True), the system back swipe gesture is no longer available and cannot be re-enabled in a supported way.

To solve this, I implemented an example of a custom back swipe gesture that feels natural on iOS, without touching the RootPanel or relying on undocumented APIs.

The Idea is:
1. Page.RootPanel is not moved: The RootPanel is system-managed (safe areas, rotation, keyboard, resize). Animating or shifting it is unreliable.
2. Wrap all content in a movable viewport panel: The actual UI is placed inside a pnlViewport. Only this panel is animated during the swipe.
3. Use a thin, partially hidden edge panel as a gesture sensor: A small panel on the left edge captures touch events. It is not UI, only an interaction zone
4. Trigger back navigation by distance OR velocity: Slow drag works with distance threshold, Fast swipe works with velocity threshold

B4X:
    mPage.Initialize("Page")
    pnlRoot = mPage.RootPanel
    pnlRoot.Visible = False
    
    mNavCtrl.ShowPage(mPage)
    Wait For Page_Resize(Width As Int, Height As Int)
    
    pnlViewport = xui.CreatePanel("Viewport")
    pnlSwipeNavigator = xui.CreatePanel("SwipeNavigator")
    
    pnlViewport.As(B4XView).SetLayoutAnimated(0, 0, 0, pnlRoot.Width, pnlRoot.Height)
    pnlViewport.Color = Colors.DarkGray
    pnlSwipeNavigator.As(B4XView).SetLayoutAnimated(0, -40dip, 0, 20dip, pnlRoot.Height)
    pnlSwipeNavigator.Color = Colors.LightGray
    
    pnlRoot.AddView(pnlViewport, 0, 0, pnlViewport.Width, pnlViewport.Height)
    pnlRoot.AddView(pnlSwipeNavigator, 0, 0, pnlSwipeNavigator.Width, pnlSwipeNavigator.Height)
    
    pnlViewport.LoadLayout("frmActivity")
    pnlBackground.Color = xui.Color_Red
    
    pnlRoot.Visible = True

B4X:
Private Sub SwipeNavigator_Touch(Action As Int, X As Float, Y As Float)
    If Action = pnlSwipeNavigator.ACTION_DOWN Then
        mfltTouchStartX = X
        mlngTouchStartTime = DateTime.Now
        mbolSwipeActive = True
        mfltCurrentOffsetX = 0
        Return
    Else If Action = pnlSwipeNavigator.ACTION_MOVE And mbolSwipeActive Then
        Dim fltDeltaX As Float
        fltDeltaX = X - mfltTouchStartX
    
        If fltDeltaX < 0 Then
            fltDeltaX = 0
        End If
    
        If fltDeltaX > 24dip Then
            fltDeltaX = 24dip
        End If
    
        mfltCurrentOffsetX = fltDeltaX
        pnlViewport.Left = fltDeltaX
        Return
    Else If Action = pnlSwipeNavigator.ACTION_UP And mbolSwipeActive Then
        mbolSwipeActive = False
    
        Dim lngDuration As Long
        Dim fltVelocityX As Float
    
        lngDuration = DateTime.Now - mlngTouchStartTime
    
        If lngDuration > 0 Then
            fltVelocityX = mfltCurrentOffsetX / lngDuration
        Else
            fltVelocityX = 0
        End If
    
        Dim bolNavigateBack As Boolean
        bolNavigateBack = False
    
        If mfltCurrentOffsetX >= 20dip Then
            bolNavigateBack = True
        Else If mfltCurrentOffsetX >= 15dip And fltVelocityX >= 0.6 Then
            bolNavigateBack = True
        End If
    
        If bolNavigateBack Then
            PerformBackNavigation
        Else
            CancelSwipe
        End If
    
        Return
    End If

End Sub

I hope you find the attached solution usefull for your needs.
 

Attachments

  • b4i-swpite-back.zip
    6.6 KB · Views: 7
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
If you create a full-screen app (NavigationBarVisible = False, HideBackButton = True), the system back swipe gesture is no longer available and cannot be re-enabled in a supported way.
Just call this and it's working:
B4X:
    NavControl.NavigationBarVisible = False
    NavControl.As(NativeObject).GetField("interactivePopGestureRecognizer").RunMethod("setDelegate:",Array(Null))
 
Upvote 0
Top