ios – SwiftUI: Failure to Dismiss a View


I have a view which is presented when a network connection fails.
It is called from inside ContentView.NavigationStack:

if !networkMonitor.isConnected {
   NetworkTimeout()
}

The Warning View itself is:

struct NetworkTimeout: View {
    @Environment(\.dismiss) var dismiss
    @Environment(\.presentationMode) var presentation

    var body: some View {
        VStack{
            Image("netError", bundle: nil)
            Text("The Network is not connected or has timed out")
                .font(.title)
                .frame(width: 300)
                .padding()
            Text("Please check your WiFi or Cell connection, then try again in a few minutes")
                .padding()
                .frame(width: 260)
            Button("Understood", action: {
                dismiss()
                self.presentation.wrappedValue.dismiss()
            })
            .buttonStyle(.borderedProminent)
        }
    }
}

I know the dismiss() and self.presentation…dismiss() are redundant. Viewing them in LLDB shows that are the same. Interestingly, dismiss shows that its value of PresentationMode.isPresented is “false”! despite the view being on screen and the Environment being declared in the current view (As per Apple docs).

Tapping the Understood button executes its action, but the Views stays presented, despite restoration of an active network.

Can someone please enlighten me?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img