Here I want to make sections in side the navigation SideBar of a NavigationSplitView. If I add sections the links are not working anymore, The view is not opening inside the detail view; why and how to solve this?
I use this as sample code to show what goes wrong.
import SwiftUI
enum Navigation2: String, Hashable, CaseIterable {
case bike = "Fiets"
case car = "Bak Met Wielen"
case bus = "Komt zo"
static let allNavigationCases = Navigation.allCases.map { $0.rawValue }
}
struct MainNavigationSplitView: View {
@State private var selection: String?
var body: some View {
NavigationSplitView {
// Sidebar view
Text("Nav").bold()
// **next two line gives issues and make the navigtionlink not working**
ForEach (Navigation2.allNavigationCases, id: \.self) { section in
Section(section ){
List(selection: $selection) {
NavigationLink(value: section) {
Label("View 3", systemImage: "3.circle")
}
NavigationLink(destination: View1(), tag: "View1", selection: $selection) {
Label("View 1", systemImage: "1.circle")
}
NavigationLink(destination: View2(), tag: "View2", selection: $selection) {
Label("View 2", systemImage: "2.circle")
}
NavigationLink(destination: View3(), tag: "View3", selection: $selection) {
Label("View 3", systemImage: "3.circle")
}
}
.listStyle(SidebarListStyle())
.frame(minWidth: 200)
.navigationTitle("Sidebar")
} header: {
Text("Navigable rows \(section)")
}
}
} detail: {
// Detail view
if let selection = selection {
Text("This is the detail view for \(selection)")
.navigationTitle(selection)
} else {
Text("Please select something from the sidebar")
.foregroundColor(.gray)
}
}
}
}
// first type of view
struct View1: View {
var body: some View {
Text("This is view 1")
.navigationTitle("View 1")
}
}
// second type of view
struct View2: View {
var body: some View {
Text("This is view 2")
.navigationTitle("View 2")
}
}
// second type of view
struct View3: View {
var body: some View {
Text("This is view 3")
.navigationTitle("View 3")
}
}
What should I do to use sections? If I use that as showed sometimes it works when other sections are closed. If more then one is open the links doesn't work.The views are just examples to show what is going wrong. Thank you
There where more semi issues then using the old way of the NavigationLink.
This partly the way I want to go and it works for now.
using the following data
I place it here in case some one may need it. Of course if someone can simplify this don't hold your horses.