Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Level Navigation SwiftUI

Tags:

swiftui

I have a master detail application I'm working on in SwiftUI but once on the 1st DetailView, NavigationLink in the NavBar no longer works. I wrote this as a simple demonstration:

struct NavView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: Layer()) {Text("Go to Layer 1")}
        }
    }
}

struct Layer: View {
    var body: some View {
        Text("Welcome to Layer 1")
            .navigationBarItems(trailing: NavigationLink(destination: AnotherLayer()) { Text("Go to Layer 2") })
    }
}

struct AnotherLayer: View {
    var body: some View {
        Text("Welcome to Layer 2")
    }
}

Everything renders and you can tap the navigationBarItem in Layer but nothing happens.

What's going on here? How can I access AnotherLayer?

like image 678
Brandon Bradley Avatar asked Dec 08 '25 09:12

Brandon Bradley


1 Answers

A NavigationLink used as a navigationBarItem will not work no matter if you use it in the first or second level but a NavigationDestinationLink would solve the problem.

import SwiftUI

struct TestSwift: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: Layer()) {Text("Go to Level")}
        }
    }
}

struct Layer: View {
   let detailView = NavigationDestinationLink(AnotherLayer())

    var body: some View {
        VStack {
           Text("Text")

        }
        .navigationBarItems(trailing:
            Button(action: {
                self.detailView.presented?.value = true
            }, label: {
                Text("Present Now!")
            })
        )
    }
}

struct AnotherLayer: View {

    var body: some View {
        Text("Hello")
    }
}
like image 92
Marc T. Avatar answered Dec 11 '25 10:12

Marc T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!