0

Hello I have a simple messaging app and I am trying to implement a left edge swipe gesture to the right in order to navigate out of the ChatView and back to the MessagesHomeView. The problem I'm running into is picking up these gestures over the scroll view. Since the chat view is one big scroll view except for a top header I have to detect edge swipes from the scroll view as well. When I add the .gesture block to the overall view, only drags on the top header are picked up and the code is executed correctly. But edge drags are not detected from the scroll view portion. Therefor I tried adding the .gesture block to the scroll view but when I try this the screen freezes and the app stops working. I would appreciate any help in detecting these edge drags off the scroll view.

import SwiftUI

struct ChatView: View {
   @State private var offset = CGSize.zero
   var body: some View {
       VStack {
           
           VStack{
               Text("header")
           }.frame(height: 80)
           
           ScrollView {
               LazyVStack{
                  
               }
           }.gesture (
               DragGesture()
                   .onChanged { gesture in
                       if gesture.startLocation.x < CGFloat(40.0){
                           if gesture.translation.width > 0 {
                               offset = gesture.translation
                               if offset.width > 200.0 {
                                   leaveView()
                               }
                           }
                       }
                   }.onEnded { _ in
                       withAnimation{
                           offset = CGSize.zero
                       }
                   }

           )
       }
       .offset(x: offset.width)
   }
}
ahmed
  • 341
  • 2
  • 9
  • Put your gesture on your VStack – devdchaudhary Aug 08 '23 at 21:06
  • @devdchaudhary I did and that also did not work. – ahmed Aug 08 '23 at 21:11
  • I would recommend to just put it in a navigationstack, that way you won't have to use a gesture. – devdchaudhary Aug 09 '23 at 06:15
  • @devdchaudhary thanks for the input. Can you show how in a brief example. Im not very familiar with navigation stacks. Id appreciate it. – ahmed Aug 09 '23 at 06:58
  • @devdchaudhary I added the MessagesHomeView into a Navigation Stack and then I navigate to the ChatView using a navigation link. However the edge swipe still does not work. I followed a tutorial on using NavigationStacks so I believe I did it right. Are you sure a navigation stack even works here? – ahmed Aug 09 '23 at 20:37
  • yes it allows you to swipe back with a gesture by default, okay let me come up with an example and give it in an answer. – devdchaudhary Aug 09 '23 at 23:19

1 Answers1

0

This is how you can swipe back to your MessagesHomeView with a NavigationStack

struct MessagesHomeView: View {
    
   var body: some View {
       NavigationStack {
           VStack {
               Spacer()
               NavigationLink(destination: ChatView()) {
                   Text("Navigate to Chat")
               }
               Spacer()
           }
       }
   }
}


struct ChatView: View {
    
   var body: some View {
       VStack {
           
           VStack{
               Text("Chats")
           }
           .frame(height: 80)
           
           ScrollView {
               LazyVStack{
                  
               }
           }
       }
   }
}
devdchaudhary
  • 467
  • 2
  • 13
  • It seems like I was already doing this but adding in " .navigationBarBackButtonHidden(true)" prevents the edge swipe. Do you know how to get ride of the navigation back button but keep the edge swipe? – ahmed Aug 09 '23 at 23:56
  • The solution for my comment above is here: https://stackoverflow.com/questions/59921239/hide-navigation-bar-without-losing-swipe-back-gesture-in-swiftui – ahmed Aug 09 '23 at 23:58
  • Yes, the extension that overrides UINavigationController is perfectly fine. – devdchaudhary Aug 10 '23 at 08:04