macOS SwiftUI Development
Overview
Create native macOS applications using SwiftUI with proper structure, navigation patterns, and macOS-specific UI components.
Project Structure
Create files in the working directory with the following structure:
code
AppName/ ├── AppName/ │ ├── AppNameApp.swift # Main app entry point │ ├── ContentView.swift # Main content view │ ├── Views/ # Additional views │ └── Models/ # Data models └── Info.plist # App configuration
App Type Selection
Choose the appropriate template based on the user's request:
Window-Based Apps
Use assets/templates/BasicWindowApp.swift when creating:
- •Standard macOS applications with windows
- •Apps with sidebar navigation
- •Document-based applications
- •Multi-window applications
Menu Bar Apps
Use assets/templates/MenuBarApp.swift when creating:
- •Apps that live in the menu bar
- •Status bar utilities
- •Background monitoring tools
- •Quick-access tools
Quick Start Workflows
Creating a Basic Window App
- •Copy the BasicWindowApp.swift template from
assets/templates/ - •Rename the struct and update app name references
- •Customize the ContentView with the desired layout
- •Add NavigationSplitView for sidebar navigation if needed
- •Include Info.plist from
assets/templates/Info.plist
Example:
swift
// Copy and modify BasicWindowApp.swift
@main
struct MyMacApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Creating a Menu Bar App
- •Copy the MenuBarApp.swift template from
assets/templates/ - •Update the app name and status bar icon
- •Customize the PopoverContentView with desired UI
- •Modify SettingsView for app preferences
- •Use Info-MenuBar.plist (with LSUIElement set to true)
Key components:
- •
@NSApplicationDelegateAdaptorfor AppDelegate - •NSStatusItem for menu bar presence
- •NSPopover for displaying UI
- •Settings scene for preferences window
Adding Keyboard Shortcuts
Add to the App struct:
swift
.commands {
CommandMenu("Actions") {
Button("New Item") {
createNewItem()
}
.keyboardShortcut("n", modifiers: .command)
Button("Delete") {
deleteItem()
}
.keyboardShortcut(.delete)
}
}
Reference Documentation
Consult these references for detailed patterns and examples:
- •
references/swiftui-patterns.md: Comprehensive SwiftUI patterns for macOS including navigation, windows, forms, tables, toolbars, and more - •
references/menubar-apps.md: Complete guide for menu bar app development including popovers, menus, icons, and launch at login
Common Patterns
Navigation with Sidebar
swift
NavigationSplitView {
List {
NavigationLink("Section 1", destination: Section1View())
NavigationLink("Section 2", destination: Section2View())
}
} detail: {
Text("Select an item")
}
Toolbar
swift
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Add") {
addItem()
}
}
}
Settings Window
swift
Settings {
SettingsView()
}
Forms
swift
Form {
Section("General") {
TextField("Name:", text: $name)
Toggle("Enabled", isOn: $enabled)
}
}
.formStyle(.grouped)
macOS-Specific Considerations
- •Window Sizing: Always set minimum window sizes with
.frame(minWidth:minHeight:) - •Menu Bar: Use template images for automatic dark mode support
- •Keyboard Navigation: Ensure all interactive elements support keyboard access
- •Settings: Provide a Settings scene for user preferences
- •Info.plist: Use
LSUIElement: truefor menu bar-only apps - •SF Symbols: Use system symbols for consistent iconography
Workflow
- •Determine app type (window-based or menu bar)
- •Copy appropriate template from
assets/templates/ - •Customize app name and bundle identifier
- •Build out UI using SwiftUI components
- •Add keyboard shortcuts and menus as needed
- •Consult reference files for specific patterns
- •Include proper Info.plist configuration
Resources
assets/templates/
- •
BasicWindowApp.swift- Standard window-based app with sidebar navigation - •
MenuBarApp.swift- Complete menu bar app with popover and settings - •
Info.plist- Standard app configuration - •
Info-MenuBar.plist- Menu bar app configuration (LSUIElement: true)
references/
- •
swiftui-patterns.md- Comprehensive SwiftUI patterns for macOS - •
menubar-apps.md- Menu bar app development guide