Andrei Herford
Andrei Herford

Reputation: 18765

Is there any advantage of using NavigationStack when working with .sheet or .fullScreenCover navigation?

After working with UIKit for several years I am still quite new to SwiftUI and especially to its navigation features.

Currently I am trying to get my head around NavigationStack and .navigationDestination. It seems that that using these features allows push navigation (like in UINavigationController) only. Is this correct?

When searching for ways to use NavigationStack with .sheet and/or .fullScreenCover navigation instead I find several examples which state that this is possible but has to be implemented manually:

struct ContentView: View {
    @State private var navigationPath = NavigationPath()
    @State private var presentingSheet: Bool = false
    @State private var presentingFullScreen: Bool = false
    @State private var selectedItem: String?

    var body: some View {
        NavigationStack(path: $navigationPath) {
            VStack {
                /*Button("Push to Detail") {
                    navigationPath.append("Detail")
                }*/

                Button("Present as Sheet") {
                    selectedItem = "Detail"
                    presentingSheet = true
                }

                Button("Present as FullScreen") {
                    selectedItem = "Detail"
                    presentingFullScreen = true
                }
            }
            /*.navigationDestination(for: String.self) { value in
                DetailView(value: value)
            }*/
            .sheet(isPresented: $presentingSheet) {
                if let selectedItem {
                    DetailView(value: selectedItem)
                }
            }
            .fullScreenCover(isPresented: $presentingFullScreen) {
                if let selectedItem {
                    DetailView(value: selectedItem)
                }
            }
        }
    }
}

While this obviously works, I do not understand what advantage I get from using NavigationStack in this example. NavigationPath is only utilized when using the push navigation but not when using .sheet or .fullStackCover, is it?

So, is there any reason to use NavigationStack at all, when push navigation is not used?

Is there any iOS/SwiftUI native solution when working with .sheet or .fullStackCover or to I need a custom or third party solution like the Routing Pattern?

Upvotes: 0

Views: 84

Answers (1)

lorem ipsum
lorem ipsum

Reputation: 29614

In SwiftUI NavigationStack is unrelated to the sheet and fullScreenCover.

NavigationLink is like show with the controller.

If you want the global show and push or present you do need a custom setup. SwiftUI does not have a global push.

Upvotes: 0

Related Questions