Skip to content

onAppear called immediately after push/pop #19

Closed
@spifd

Description

@spifd

Hi there,

First, thanks for this stack, really useful for my small side project as a SwiftUI rookie.

Actually, I've just encountered the following behavior: onAppear is called immediately when pushing/popping pages, and not when the transition is finished. As I have an advanced custom transition taking more time and leading to a page that will have its own animation, I would like onAppear to be delayed until the transition is finished.

I believe this is not really a bug but I'm wondering if this could be solved by this stack. Note: I can probably use state and enhance my custom transition to workaround (even though tbh custom transitions are a bit buggy when I want to deal with timing/delays).

Here is a simple example:

struct DestinationView: View {
    var body: some View {
        VStack {
            Spacer()
            HStack {
                PopView {
                    Text("Page 2").onAppear {
                        print("Page 2 onAppear") // Printed immediately
                    }
                }
            }
            Spacer()
        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
    }
}

struct Transition_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            NavigationStackView(transitionType: .default,
                                easing: Animation.linear(duration: 5)) {
                                    PushView(destination: DestinationView()) {
                                        HStack {
                                            Spacer()
                                            Text("Page 1").onAppear {
                                                print("Page 1 onAppear") // Printed immediately
                                            }
                                            Spacer()
                                        }
                                    }
            }
        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions