Swiftui MACos Xcode Style Toolbar

SwiftUI macOS Xcode Style Toolbar

Okay, I found a trick that works:

  1. Set the scene's windowStyle to HiddenTitleBarWindowStyle, which both hides the title and removes the white background:
WindowGroup {
ContentView()
}
.windowToolbarStyle(UnifiedCompactWindowToolbarStyle())
.windowStyle(HiddenTitleBarWindowStyle())

(Note that I don't set the scene name to an empty string, as that's no longer needed and it messed up the window name in the "Window" menu too)


  1. To force a divider between the toolbar and the detail view content, stretch the detail content to fill the whole space and put a Divider behind it:
Text("Detail")
.frame(minWidth: 200, maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.background(VStack {
Divider()
Spacer()
})
.toolbar { ...

That seems to do it!

Sample Image

How do you create a Preferences Style toolbar in SwiftUI for macOS?

Are you using the SwiftUI App Lifecycle? If so, add a TabView to your Settings view and the tabs will appear in the toolbar automatically:

Settings {
TabView {
PreferencesView()
.tabItem {
Label("General", systemImage: "gearshape")
}
}
}

If you’re not using the SwiftUI app lifecycle, then I think you’ll need to build this style of toolbar manually using NSToolbar.



Related Topics



Leave a reply



Submit