MacOS Development Handbook
A Common Mac Application with a Sidebar and Content Area
NavigationView as the Base View
In our main content view, we can use NavigationView as our base body. A navigation view in MacOS will display two separated views, a sidebar at left and content view at right.
1 | NavigationView { |
Of course, the window’s frame definition is necessary for appropriate window layout.
Creating a List as the Sidebar
The List component in SwiftUI is used to create a list view with multiple item cells.
1 | List(selection: $selection) { |
We can use ForEach to iterate through all our cell data and display them in the list view.
We use Section to separate our cells into several different groups, these groups can be collapsed respectively.
Setting the list style by using the modifier of listStyle. There are many build-in list styles you can use. More details you can check the official document List Styles
Changing the Cursor Over Links
In desktop devices, users normally use mouses or tack pads for controlling the cursor. When the cursor moves over a clickable element in our applications, we should give some tips for indicating you can click it. In SwiftUI, the onHover modifier is designed for this.
1 | .onHover { inside in |
The block will be invoked whenever the cursor move in or out the decorated view’s area.
Sand Box
In iOS, every application runs in an isolated sand box. The application doesn’t have access of data of other applications. But in MacOS, applications can share data with the file system. For accessing local or remote data, we have to enable the App Sandbox capability by check the options in Target -> Signing & Capabilities -> App Sandbox.
Checking the Outgoing Connections (Client) box, if you just want to make a network request with URLSession.
Adding Menus
If you want to add some menus for your application, you need to add a commands modifier to the WindowGroup view. The modifier needs you to return some Commands views. The Commands is a protocol and we can construct a new View struct complying it to customize ourselves menus.
There are many build-in commands we can use directly. (Commands)[https://developer.apple.com/documentation/swiftui/commands]
- EmptyCommands
- ImportFromDevicesCommands
- SidebarCommands
- TextEditingCommands
- TextFormattingCommands
- ToolbarCommands
There are two exceptive items, CommandGroup and CommandMenu. They are both used to make new command menus, but the CommandMenu is for creating a stand-alone top level menu, the CommandGroup is for adding additional commands to existing commands menus.
1 | struct Menus: Commands { |
In command menus, we usually use Toggle, Picker and Button to crate our custom controls. The Toggle is for boolean values, the Picker is for enum values, the Button is for normal commands.
We can use the .keyboardShortcut modifier to add shortcut to our commands.
AppStorage and SceneStorage
Swift provides two tools for easily writing, reading or observing UserDefaults.
The AppStorage saves preferences of the whole application. The SceneStorage saves preferences separately for multiple windows for the application.
Adding Toolbars
We usually add a toolbar onto the content view, so we add a toolbar modifier to the DetailContentView. Like the commands we used above, the toolbar modifier needs us to return ToolbarContent views.
1 | struct Toolbar: ToolbarContent { |