SwiftUI macOS View toolbar not showing in a new window

Viewed 350

I have the following view in my macOS app:

struct ViewWithToolbar: View {
    var body: some View {
        Group {
            Text("Hello from ViewWithToolbar!")
                .padding()
        }
        .toolbar {
            Button("Action1") {  }
            Button("Action2") {  }
        }
        .frame(minWidth: 500, minHeight: 400.0)
    }
}

If I pass this view in the App declaration, everything works as expected. The problem begins when I tried to present this view in a new window, like in the following example:

struct RootView: View {
    var body: some View {
        Group {
            Button("Present View with Toolbar") {
                let controller = NSHostingController(rootView: ViewWithToolbar())
                let window = NSWindow(contentViewController: controller)
                window.contentViewController = controller
                window.title = "View with Toolbar"
                window.styleMask = [.titled, .closable, .miniaturizable, .resizable]
                window.makeKeyAndOrderFront(nil)
            }
            .padding()
        }
        .frame(minWidth: 500, minHeight: 400)
    }
}

The view does open in a new window, however the toolbar is not showing anymore. It seems to ignore the top bar altogether, apart from the title I pass in to the window.

What could be causing this issue? Does it have to do with the way I'm presenting the window? Is there a more SwiftUI'ish way of presenting a new window that can solve this?

0 Answers
Related