SwiftUI - macOS Big Sur - How to move ToolbarItem to corresponding right view

Viewed 2120

I'm trying to create a three column window with a sidebar and a splitview. Each of the left and right views in the split view should have a corresponding ToolbarItem that display in their respective toolbar/navigationtitle space.

For this purpose, I created a simple view that has a sidebar, left and right views. I created a ToolbarItem for left view and another ToolbarItem for right view. However, the ToolbarItem belonging to the right view shows in the left view's toolbar space. Is there a way to create ToolbarItem's specific to each of the views. Any help is greatly appreciated.

Below is the source code that I created. I've also attached a screenshot showing the issue. I'm using Xcode 12 beta and macOS Big Sur Beta.

Toolbar issue

import SwiftUI

struct ContentView: View {
    enum NavigationItem {
        case demo
    }
    @State private var selection: Set<NavigationItem> = [.demo]

    var body: some View {
        NavigationView {
                List(selection: $selection) {
                    Label("Demo", systemImage: "list.bullet")
                }
                .listStyle(SidebarListStyle())
                .frame(minWidth: 200, idealWidth: 250, maxWidth: 300, maxHeight: .infinity)

            Text("Left")
                .frame(maxWidth: 500, maxHeight: .infinity)
                .toolbar {
                    ToolbarItem {
                        Button(action: {}) {
                            Text("Left Toolbar")
                        }
                    }
                    ToolbarItem { Spacer() }
                }
                .presentedWindowToolbarStyle(ExpandedWindowToolbarStyle())

            Text("Right")
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .toolbar {
                    ToolbarItem {
                        Button(action: {}) {
                            Text("Right Toolbar")
                        }
                    }
                }
        }
    }
}
0 Answers
Related