A preferences window that adheres to the macOS Human Interface Guidelines.
A preferences window suitable for a macOS app. Its user experience adheres to the macOS Human Interface Guidelines.
Requires Swift 5.3+. Tested on macOS 11. MIT license.
class GeneralPreferencePaneViewController: NSViewController, PreferencePane, ToolbarItemImageProvider {
init() {
// …
title = NSLocalizedString("General",
comment: "The title of the General preference pane.")
}
let preferencePaneIdentifier = PreferencePaneIdentifier(rawValue: "general")
let toolbarItemImage = NSImage(named: NSImage.preferencesGeneralName)!
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
private let preferencesWindowController: PreferencesWindowController = {
let preferencePaneViewControllers: [NSViewController & PreferencePane & ToolbarItemImageProvider] = [
GeneralPreferencePaneViewController(),
// …
]
return PreferencesWindowController(preferencePaneViewControllers: preferencePaneViewControllers)
}()
@IBAction
private func showPreferencesWindow(_ sender: Any) {
preferencesWindowController.showWindow(self)
}
}
Important: Ensure that each of your preference panes has an unambiguous and fixed width and height, explicitly via width/height constraints and/or implicitly via the constraints of the subviews.