Why does SwiftUI automatically disable all buttons when dismissing a sheet and then changing the NavigationPath?

Viewed 28

This example consists of a simple NavigationStack leading to Subview. The latter displays SomeSheet which, when its button is tapped, dismisses itself and changes the NavigationPath in order to go back to ContentView.

The mechanism works perfectly. However, all the buttons in ContentView are automatically disabled (example in video). Why does this happen? Is it a SwiftUI bug?

enum Root: Hashable {
    case subview
}

struct ContentView: View {
    @StateObject var model = Model()
    
    var body: some View {
        NavigationStack(path: $model.path) {
            List {
                Button("Some button") {} // FIXME: All ContentView buttons are disabled when SomeSheet's button is tapped
                NavigationLink(value: Root.subview) {
                    Text("Go to Subview")
                }
            }
            .navigationDestination(for: Root.self) { _ in Subview() }
        }
        .environmentObject(model)
    }
}

extension ContentView {
    @MainActor final class Model: ObservableObject {
        @Published var path = NavigationPath()
    }
}

struct Subview: View {
    @State private var sheetIsPresented = false
    
    var body: some View {
        Button("Show sheet") { sheetIsPresented = true }
            .sheet(isPresented: $sheetIsPresented) { SomeSheet() }
    }
}

struct SomeSheet: View {
    @EnvironmentObject var model: ContentView.Model
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        Button("Dismiss and go back to ContentView") {
            dismiss()
            model.path.removeLast()
        }
    }
}

Tested with Xcode 14.0 (iOS 16).

0 Answers
Related