8

As detailed here (on an iOS topic), the following code can be used to make a SwiftUI View dismiss itself:

@Environment(\.presentationMode) var presentationMode
// ...
presentationMode.wrappedValue.dismiss()

However, this approach doesn't work for a native (not Catalyst) macOS NavigationView setup (such as the below), where the selected view is displayed alongside the List.

Ideally, when any of these sub-views use the above, the list would go back to having nothing selected (like when it first launched); however, the dismiss function appears to do nothing: the view remains exactly the same.

Is this a bug, or expected macOS behaviour?

Is there another approach that can be used instead?

struct HelpView: View {

    var body: some View {

        NavigationView {
            List {
                NavigationLink(destination:
                    AboutAppView()
                ) {
                    Text("About this App")
                }
                NavigationLink(destination:
                    Text("Here’s a User Guide")
                ) {
                    Text("User Guide")
                }
            }
        }
    }
}

struct AboutAppView: View {

    @Environment(\.presentationMode) var presentationMode

    public var body: some View {
        Button(action: {
            self.dismissSelf()
        }) {
            Text("Dismiss Me!")
        }
    }

    private func dismissSelf() {
        presentationMode.wrappedValue.dismiss()
    }
}

FYI: The real intent is for less direct scenarios (such as triggering from an Alert upon completion of a task); the button setup here is just for simplicity.

TheNeil
  • 3,321
  • 2
  • 27
  • 52
  • 2
    Referred topic was for iOS, just in case. – Asperi Dec 16 '19 at 17:43
  • Yeah, noted, thank you. Hopefully most SwiftUI functionality works across both, but this appears not to be one of those situations. I think I saw comments suggesting it doesn’t work for iPadOS split view either, but I’m not certain on that first hand. – TheNeil Dec 16 '19 at 17:45
  • 1
    I’ve also made that OS variance explicit in the question now. – TheNeil Dec 16 '19 at 17:46
  • @TheNeil any Idea, how to solve this in MacOS in the meantime? – mica Apr 05 '20 at 20:36
  • Hi @mica. Unfortunately, no. Haven't seen any answer come in for this one. Honestly, I'm mostly working around it by using more conditional views and steering away from navigation views, but that's far from ideal. – TheNeil Apr 06 '20 at 15:07

1 Answers1

2

The solution here is simple. Do not use Navigation View where you need to dismiss the view.

Check the example given by Apple https://developer.apple.com/tutorials/swiftui/creating-a-macos-app

If you need dismissable view, there is 2 way.

  1. Create a new modal window (This is more complicated)
  2. Use sheet.

Following is implimenation fo sheet in macOS with SwiftUI

struct HelpView: View {
    @State private var showModal = false
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination:
                    VStack {
                        Button("About"){ self.showModal.toggle() }
                        Text("Here’s a User Guide")
                    }
                ) {
                    Text("User Guide")
                }
            }
        }
        .sheet(isPresented: $showModal) {
            AboutAppView(showModal: self.$showModal)
        }
    }
}

struct AboutAppView: View {
    @Binding var showModal: Bool
    public var body: some View {
        Button(action: {
            self.showModal.toggle()
        }) {
            Text("Dismiss Me!")
        }
    }
}

There is also a 3rd option to use ZStack to create a Modal Card in RootView and change opacity to show and hide with dynamic data.

NikzJon
  • 912
  • 7
  • 25