4

I want to present a view with a NavigationStack that has an initial path. This almost always works, but not when that view is presented as a sheet. Then, it only shows the root view of the NavigationStack.

Here is a fully working example:

struct TestView: View {
    @State var isShowingSheet = false
    @State var isShowingFullscreenCover = false

    var body: some View {
        VStack {
            Button("Show Sheet") {
                isShowingSheet = true
            }
            Button("Show Fullscreen Cover") {
                isShowingFullscreenCover = true
            }
        }
        .sheet(isPresented: $isShowingSheet) {
            // Opens at the root (Showing the "Root" text below)
            PresentedView()
        }
        .fullScreenCover(isPresented: $isShowingFullscreenCover) {
            // Opens with page "5", which is the top of the intially defined navigation stack from `path`
            PresentedView()
        }
    }
}

struct PresentedView: View {
    @State var path: [Int] = [1, 2, 3, 4, 5]

    var body: some View {
        NavigationStack(path: $path) {
            Text("Root")
                .navigationDestination(for: Int.self) { number in
                    Text("\(number)")
                        .navigationTitle("\(number)")
                }
        }
    }
}

Sheet presentation:

enter image description here

FullScreenCover presentation:

enter image description here

Is this a bug in SwiftUI or am I missing something? Is this intentional? Does anyone know a fix/workaround?

1 Answer 1

2

To me this is a bug.

Here is a workaround setting the path in .onAppear. But watch the still different behavior of sheet and fullscreencover.

The strangest thing is that for sheet to work, the path has to be initially empty !?

struct PresentedView: View {
    @State var path: [Int] = []

    var body: some View {
        NavigationStack(path: $path) {
            Text("Root")
                .navigationDestination(for: Int.self) { number in
                    Text("\(number)")
                        .navigationTitle("\(number)")
                }
                .navigationTitle("Root")
        }
        .onAppear {
            path = [1,2,3,4,5]
        }
    }
}
3
  • Okay, that's kind of hilarious. Setting it in .onAppear makes it work fine in a sheet, but now the fullScreenCover opens at the root (and then animates a push animation) :D Yeah, it seems the path has to be initially empty for the sheet, which makes it seemingly impossible to have both sheets and fullScreenCover work at the same time with this workaround :/ Super strange, but appreciate your help! Jan 15, 2023 at 15:05
  • 2
    Just to be sure, I submitted it as a bug: FB11946726 Jan 15, 2023 at 15:06
  • Currently the below code makes it seemingly impossible to have both sheets and fullScreenCover work at the same time, as well in my iOS 17 only app! .sheet(isPresented: $showDetailView) { NavigationStack { DetailView(isShowingBottomSheet: $isShowingBottomSheet) .navigationTitle("Detail") } } Nov 12, 2023 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.