Switui - Two Navigationlink in a List

SwitUI - Two navigationLink in a list

Try the following approach - the idea is to hide links in background of visible content and make them inactive for UI, but activated programmatically.

Tested with Xcode 12 / iOS 14.

struct MultiNavLink: View {

var body: some View {
return
NavigationView {
List {
OneRowView()
}.navigationBarTitle("MultiNavLink", displayMode: .inline)
}
}
}

struct OneRowView: View {
@State var mb_isActive1 = false
@State var mb_isActive2 = false

var body: some View {
ZStack {
Text("Single tap::go to destination1\nDouble tap,go to destination2")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.contentShape(Rectangle())
.background(Group {
NavigationLink(destination: Text("Destination1"), isActive: $mb_isActive1) {
EmptyView() }
.buttonStyle(PlainButtonStyle())
NavigationLink(destination: Text("Destination2"), isActive: $mb_isActive2) {
EmptyView() }
.buttonStyle(PlainButtonStyle())
}.disabled(true))
.highPriorityGesture(TapGesture(count: 2).onEnded {
self.mb_isActive2 = true
})
.onTapGesture(count: 1) {
self.mb_isActive1 = true
}
}
}

SwiftUI NavigationLink issue with multi selections

I can reproduce your issue, and I think this is a bug.
However using the following NavigationLink setup with selection and tag seems to works for me.

struct ContentView: View {
@State var cars = [Car(name: "A"), Car(name: "B"), Car(name: "C"), Car(name: "D")]
@State private var showCar: UUID?

var body: some View {
NavigationView {
LazyVStack(spacing: 30) {
ForEach(cars) { car in
NavigationLink(
destination: CarDetailsView(car: car),
tag: car.id,
selection: $showCar,
label: { CarRowView(car: car) }
)
}
}
}
}
}

struct CarDetailsView: View {
@Environment(\.dismiss) private var dismiss
@State var car: Car

var body: some View {
Button("Back \(car.name)") {
dismiss()
}
}
}

struct Car: Identifiable, Hashable {
let id = UUID()
var name: String
}

struct CarRowView: View {
var car: Car

var body: some View {
Text(car.name)
}
}

Multiple NavigationLink in SwiftUI

Put your views inside a VStack:

struct Test : View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: Text("First")) {
Text("Visible")
}

NavigationLink(destination: Text("Second")) {
Text("Invisible")
}
//EDIT: Also Invisible
Text("Not rendered")
}
}
}
}


Related Topics



Leave a reply



Submit