Home > OS >  SwiftUI NavigationLink isActive Binding Not Updated/Working
SwiftUI NavigationLink isActive Binding Not Updated/Working

Time:09-28

I have the following:

@State private var showNext = false

...
    NavigationStack {
        VStack {
            NavigationLink(destination: NextView(showSelf: $showNext),
                           isActive: $showNext) { EmptyView() }

            Button("Show Next") {
                showNext = true
            }
        }
    }

...

struct NextView: View {
    @Binding var showSelf: Bool

    var body: some View {
        Text("Next")
         .navigationTitle("Next")

        Button("Dismiss") {
            showSelf = false
        }
            .padding(30)
    }
}

When tapping Show Next, the NextView is shown as expected.

But when tapping Dismiss, nothing happens.

Turns out that showSelf was already false before it's set to false. So it seems something went wrong with passing the binding into NextView.

What could be wrong?

CodePudding user response:

The issue was caused by NavigationStack. When I replaced it with NavigationView it worked as expected.

The isActive binding of NavigationLink does not appear to work (or to be supported) when embedded in a NavigationStack.

CodePudding user response:

isActive binding is for NavigationView, try:

NavigationView {
    ...
}
.navigationViewStyle(.stack)`
  • Related