mirror of
https://github.com/Dimillian/Skills.git
synced 2026-03-25 08:55:54 +00:00
Added new reference guides for building macOS Settings windows and customizing menu bar commands in SwiftUI. Updated the components index to include links to these new resources.
2 KiB
2 KiB
macOS Settings
Intent
Use this when building a macOS Settings window backed by SwiftUI's Settings scene.
Core patterns
- Declare the Settings scene in the
Appand compile it only for macOS. - Keep settings content in a dedicated root view (
SettingsView) and drive values with@AppStorage. - Use
TabViewto group settings sections when you have more than one category. - Use
Forminside each tab to keep controls aligned and accessible. - Use
OpenSettingsActionorSettingsLinkfor in-app entry points to the Settings window.
Example: settings scene
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
#if os(macOS)
Settings {
SettingsView()
}
#endif
}
}
Example: tabbed settings view
@MainActor
struct SettingsView: View {
@AppStorage("showPreviews") private var showPreviews = true
@AppStorage("fontSize") private var fontSize = 12.0
var body: some View {
TabView {
Form {
Toggle("Show Previews", isOn: $showPreviews)
Slider(value: $fontSize, in: 9...96) {
Text("Font Size (\(fontSize, specifier: "%.0f") pts)")
}
}
.tabItem { Label("General", systemImage: "gear") }
Form {
Toggle("Enable Advanced Mode", isOn: .constant(false))
}
.tabItem { Label("Advanced", systemImage: "star") }
}
.scenePadding()
.frame(maxWidth: 420, minHeight: 240)
}
}
Skip navigation
- Avoid wrapping
SettingsViewin aNavigationStackunless you truly need deep push navigation. - Prefer tabs or sections; Settings is already presented as a separate window and should feel flat.
- If you must show hierarchical settings, use a single
NavigationSplitViewwith a sidebar list of categories.
Pitfalls
- Don’t reuse iOS-only settings layouts (full-screen stacks, toolbar-heavy flows).
- Avoid large custom view hierarchies inside
Form; keep rows focused and accessible.