@Environment(\.presentationMode) Broken?

Has anyone gotten @Environment(\.presentationMode) to work correctly on a device?


When I try and use:

self.presentation.value.dismiss()


on a page pushed from a navigation link it works on the simulator but crashes on device.

import SwiftUI
import Combine
struct ContentView: View {
    @State private var showModal = false
    @State private var showCamera = false
    var body: some View {
        NavigationView {
            Text("Hello World")
                .navigationBarTitle("", displayMode: .inline)
                .navigationBarItems(
                    leading:
                        NavigationLink(
                            destination: ModalView(message: "Dismiss Push Test"),
                            label: {Text("Push Page")}),
                    trailing:
                    Button("Show modal") {
                        self.showModal = true
                    }.sheet(isPresented: $showModal, onDismiss: {
                    print(self.showModal)
                }) {
                    ModalView(message: "Dismiss Modal view")
                }
            )
        }
    }
}

struct ModalView: View {
    @Environment(\.presentationMode) var presentation
    let message: String

    var body: some View {
        NavigationView {
            Button(message) {
                self.presentation.value.dismiss()
            }
            .navigationBarItems(trailing: Button("Done") {
                self.presentation.value.dismiss()
            })
        }
    }
}

Same happens to me. Have you filed a bug on it?

It should be
Code Block
self.presentation.wrappedValue.dismiss()

This is completely normal, you are trying to dismiss a view which is the root view of a NavigationView,

You should remove the NavigationView inside your ModalView and also change

self.presentation.value.dismiss() to self.presentation.wrappedValue.dismiss()

@Environment(\.presentationMode) Broken?
 
 
Q