SwiftUI: how to use NavigationView in macOS?

Here is my code that seems to fix it using Xcode 12.2 beta 3:

import SwiftUI

var listItems = ["Item 1", "Item 2", "Item 3", "Item 4"]
var secondItems = ["Second 1", "Second 2", "Second 3", "Second 4"]

struct ContentView: View
{
    
    @State var select: String? = "Item 1"
    var body: some View
    {
        VStack
        {
            NavigationView
            {
                List
                {
                    ForEach((0..<listItems.count), id: \.self)
                    {index in
                        NavigationLink(destination: SecondView(), tag: listItems[index], selection: $select)
                        {
                            Text(listItems[index])
                                .padding(.vertical, 2.0)
                        }
                    }
                    Spacer()
                    
                }.frame(width:160)
                                .listStyle(SidebarListStyle())
            }
            
            .toolbar
            {
                Text("this is not the title")
                Button(action: {})
                {
                    Label("Upload", systemImage: "square.and.arrow.up")
                }
            }
            .navigationTitle("My Title")
            .navigationViewStyle(DoubleColumnNavigationViewStyle())
        }
    }
}

struct SecondView: View {

    var body: some View {
        NavigationView {
            List
            {
                ForEach((0..<secondItems.count), id: \.self)
                {index in
                    NavigationLink(destination: Text(secondItems[index]))
                    {
                        Text(secondItems[index])
                            .frame(height: 20)
                    }
                }
            }.frame(width:150)
        }
    }
}

This makes a window like this:

enter image description here


I don't have the answer but I'm trying to do the same thing and have a few observations to add, maybe they will help:

Add a destination View:

NavigationButton(destination: DetailView()) {
            Text("Show Detail")
        }

Setting a width on the NavigationView stops the right-hand view from disappearing.

Also, adding

 .onAppear { print("DetailView called") } 

to the detail view shows that, even though it isn't being displayed, the view is in fact called when the button is clicked.

Edit: it's there! The view was hidden by the divider, drag it left to see the detail view.

Edit 2: Xcode beta 2 gives a "'NavigationView' is unavailable in macOS" message.

Tags:

Ios

Macos

Swiftui