How do you fix Xcode 14 warning: NavigationLink presenting a value must appear inside a NavigationContent-based NavigationView. Link will be disabled
Asked Answered
M

4

18

Since installing Xcode 14, I am now getting the following error message printed in my console:

NavigationLink presenting a value must appear inside a NavigationContent-based NavigationView. Link will be disabled.

My app is structured as follows:

  1. I have View A wrapped in a NavigationView. The Navigation View has a navigation link inside it that links to View B.

  2. I have View B that doesn't have a Navigation View, but has a navigation link to View C. View B inherits the navigation view defined in View A

The warning is printed when I press the back button on View B, popping back to View A. The warning goes away when I wrap View B in a NavigationView, but this of course now displays View B in two Navigation Views, which is not what I want.

I'm unsure why this warning is printing, because View B inherits the NavigationView defined in View A.

Margarito answered 12/9, 2022 at 18:49 Comment(1)
Without including a minimal reproducible example, it is unlikely that this is going to be debuggable from just a description.Protonema
S
17

I had the same issue. Adding a check for iOS16 and using the new navigationstack if true fixed it for me.

WindowGroup {
    if #available(iOS 16.0, *) {
        NavigationStack {
            ContentView()
        }
    } else {
        // Fallback on earlier versions
        NavigationView {
            ContentView()
        }
    }
}
Sloth answered 23/9, 2022 at 23:45 Comment(0)
C
11

Try to add .navigationViewStyle(.stack) to the NavigationView. It helps in my case.

NavigationView {
    // View A
}
.navigationViewStyle(.stack)
Colliery answered 16/1, 2023 at 16:22 Comment(0)
C
1

Will be deprecated in ios16, The official documentation link is provided here, you can view the specific details

Deprecated

Use NavigationStack and NavigationSplitView instead. For more information, see Migrating to new navigation types.

https://developer.apple.com/documentation/swiftui/navigationview

Ceolaceorl answered 14/9, 2022 at 8:32 Comment(1)
You should quote the essential parts of the documentation and leave the link for reference. See how to write a good answer.Evictee
C
1

It isn't incredibly intelligent, but it's clever enough to rectify Apple's user-unfriendly modifications.

struct SmartNavigationView<Content>: View where Content: View {
    @ViewBuilder var content: () -> Content

    var body: some View {
        if #available(iOS 16, *) {
            NavigationStack(root: content)
        } else {
            NavigationView(content: content)
                .navigationViewStyle(.stack)
        }
    }
}

Usage

var body: some View {
    SmartNavigationView {
        NavigationLink {
            Text("Perhaps it's because they lack faith in using SwiftUI for serious projects.")
        } label: {
            Text("Why did SwiftUI developers implement these types of changes?")
        }
    }
}
Cameroncameroon answered 20/8, 2023 at 13:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.