Search code examples
iosswiftswiftuiipados

Creating two sidebars in iPadOS application using SwiftUI


I have created one sidebar with NavigationView, which by default appends to the left of the landscape view of application. However I wanted to have another on the right side.

NavigationView {
  List {
    Label("Pencil", systemImage: "pencil")
    Label("Paint", systemImage: "paintbrush.fill")
    Label("Erase", systemImage: "quote.opening")
      Label("Cutter", systemImage: "scissors")
      Label("Eyedropper", systemImage: "eyedropper.halffull")
      Label("Draw Line", systemImage: "line.diagonal")

  }
  .listStyle(SidebarListStyle())
}
    

Image of preview screen


Solution

  • Here's a simple working example made with SwiftUI:

    struct ContentView: View {
        var body: some View {
            NavigationView{
                
                TagView()
                
                Text("Default second View")
                Text("Default Third View")
            }
        }
    }
    
    
    struct TagView: View {
        let tags = ["Apple", "Google"]
        var body: some View {
            List{
                ForEach(tags, id: \.self) { name in
                    NavigationLink {
                        ProductView(tag: name)
                    } label: {
                        Text(name)
                    }
    
                }
            }
        }
    }
    
    
    struct ProductView: View {
        var tag: String
        var products: [String] {
            if tag == "Apple" {
                return ["iPhone", "iPad", "MacBook"]
            } else {
                return ["resuable stuff"]
            }
        }
        var body: some View {
            List{
                ForEach(products, id: \.self) { name in
                    NavigationLink {
                        DetailsView()
                    } label: {
                        Text(name)
                    }
    
                }
            }
        }
    }
    
    
    struct DetailsView: View {
        var body: some View {
            Text("Detailed explanation about product")
        }
    }
    

    enter image description here