NavigationView和工具栏

时间:2020-10-15 03:17:40

标签: macos swiftui

我正在建立三列NavigationView。如何使工具栏坚持特定视图?我想分别为第二列和第三列制作太棒,就像下面显示的Mail一样。

mail

我尝试使用以下代码,但是工具栏似乎附加在第二个和第三个视图的顶部。另外,如果我调整视图大小,则toolbar保持不变。 (不响应相应的视图)

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            AppSidebarNavigation()
        }
    }
}

struct AppSidebarNavigation: View {
    var sidebar: some View {
        List(selection: $selection) {
            NavigationLink(destination: SecondView()) { Text("Second View") }
        }
        .listStyle(SidebarListStyle())
    }

    var body: some View {
        NavigationView {
            sidebar
            SecondView()
            Text("no item selected")
        }
    }
}

struct SecondView: View {
    var body: some View {
        VStack {
            NavigationLink(destination: Text("Third View")) {
                Text("Third View")
            }
            Spacer()
        }
        .toolbar {
            ToolbarItemGroup(placement: .automatic) {
                Text("Title")
                Spacer()
                Button(action: {}) { Image(systemName: "plus") }
                Spacer()
            }
        }
    }
}

enter image description here

0 个答案:

没有答案