1

As you probably know, SwiftUI's .onAppear is called when the view appears.

In my application, I have implemented a swipe back gesture from child to parent view. However, parent's onAppear is called when it is already partly visible when doing a swipe back gesture.

So my question is: Any thoughts what should I do to make sure onAppear is called when parent view has appeared in full (not partly)?

liudasbar
  • 173
  • 4
  • 11

1 Answers1

0

You can check the state of the NavigationLink in the isActive Binding. Here is an example with ProxyBinding where you detect the slide back in the setter.

struct ContentView: View {
    @State var isActive: Bool = false

    var body: some View {
        NavigationView {
            NavigationLink(
                destination: Text("Destination"),
                isActive: Binding<Bool>(
                    get: {
                        isActive
                    }, set: {
                        isActive = $0
                        // Here you can check the status, if it is false it is fully slided back. 
                        print($0)
                    }),
                label: {
                    Text("Navigate")
                })
        }
        .onAppear {
            print("appear")
        }
    }
}
davidev
  • 7,694
  • 5
  • 21
  • 56