In SwiftUI, iOS15, 2nd level NavigationLink, isActive is not working
Asked Answered
P

2

6

in iOS15, it is not working:

import SwiftUI
struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink {
                Dest1().navigationTitle("Dest1")
            } label: {
                Text("to Destination 1")
            }
        }
    }
}

struct Dest1: View {
    @State var dest2Active: Bool = false
    var body: some View {
        NavigationLink(
            destination: Button {
                dest2Active = false // not working!!
            } label: {Text("dismiss")} .navigationTitle("Dest2"),
            isActive: $dest2Active
        ) {Text("to Destination 2")}
    }
}

The dismiss button in Dest2 is not working! I remember that in iOS14, this code works well. How to resolve this?

Piroshki answered 31/10, 2021 at 2:56 Comment(0)
O
6

You need to add .navigationViewStyle(.stack) to make it work. Here is the test code that works for me.

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink {
                Dest1().navigationTitle("Dest1")
            } label: {
                Text("to Destination 1")
            }
        }.navigationViewStyle(.stack) // <-- here the important bit
    }
}

struct Dest1: View {
    @State var dest2Active: Bool = false
    var body: some View {
        NavigationLink(
            destination: Button {
                dest2Active = false // now working!!
            } label: {Text("dismiss")} .navigationTitle("Dest2"),
            isActive: $dest2Active
        ) {Text("to Destination 2")}
    }
}
Oquassa answered 31/10, 2021 at 3:45 Comment(0)
M
8

Adding .isDetailLink(false) to the top level NavigationLink seems to solve the issue. Note that this works on iPhone iOS -- for iPad, you will need to use a StackNavigationStyle as @workingdog suggests in their answer.

The documentation is not clear on why this works (in fact, it refers specifically to multi-column navigation), but it seems to solve a number of NavigationLink-related issues. See, for example: https://developer.apple.com/forums/thread/667460

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink {
                Dest1()
                    .navigationTitle("Dest1")
            } label: {
                Text("to Destination 1")
            }.isDetailLink(false)
        }
    }
}

struct Dest1: View {
    @State var dest2Active: Bool = false
    var body: some View {
        NavigationLink(isActive: $dest2Active) {
            Dest2(dest2Active: $dest2Active)
        } label: {
            Text("to Destination 2")
        }
    }
}

struct Dest2: View {
    @Binding var dest2Active : Bool
    
    var body: some View {
        Button {
            dest2Active = false
        } label: {
            Text("Dismiss")
        }.navigationTitle("Dest2")
    }
}
Mainspring answered 31/10, 2021 at 3:8 Comment(0)
O
6

You need to add .navigationViewStyle(.stack) to make it work. Here is the test code that works for me.

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink {
                Dest1().navigationTitle("Dest1")
            } label: {
                Text("to Destination 1")
            }
        }.navigationViewStyle(.stack) // <-- here the important bit
    }
}

struct Dest1: View {
    @State var dest2Active: Bool = false
    var body: some View {
        NavigationLink(
            destination: Button {
                dest2Active = false // now working!!
            } label: {Text("dismiss")} .navigationTitle("Dest2"),
            isActive: $dest2Active
        ) {Text("to Destination 2")}
    }
}
Oquassa answered 31/10, 2021 at 3:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.