Swift Programming for macOS

🍎 🖥

There are plenty of books, videos, and online resources for developing iOS apps. Despite the fact that iPhone and iPad apps require a Mac for code development, there is little information about actually creating native Mac applications. The articles provided below demonstrate various aspects of Mac app development using the latest versions of Swift and SwiftUI. Hopefully these examples will provide a useful resource for Mac developers.

See the swift-macos repository on GitHub for example code.

Application should reopen window

A Mac app created by the default macOS Xcode template will release the main window when it is closed. This makes it impossible to reopen the window unless certain features are added to the app.

Text copy

The typical way to copy text is to select it with the mouse then press ⌘C. To do this in code, use the NSPasteboard class to transfer text to the pasteboard server.


The mouse pointer (or cursor) is represented by an arrow on the screen. Different cursors are available in macOS to indicate actions that the user can take with the mouse.

Button styles

Several button styles are available in macOS such as the plain, link, bordered, and borderless styles. For a truly custom style, conforming a struct to the ButtonStyle protocol can offer more customization options.

Window size

The window size is defined by the frame size of the containing view. In this example the VStack frame is set to a width of 500 and height of 300 which makes the window width 500 and height 300.

Web view

A WKWebView from the WebKit framework is used to display web content in a window. The web view can be wrapped with NSViewRepresentable to make it usable with SwiftUI. Content for the web view can be loaded using a website url, a string representing HTML content, or by loading an HTML file.

Text vertical

Vertical text can be accomplished by rotating a text view 90 degrees. To rotate the frame of the text view, the fixed size modifier must be implemented along with defining the frame size.

Text view

The text structure in SwiftUI is a view that displays lines of text. Various modifiers are available to customize the appearance and style of text.


The TextField structure is a control that provides an editable text field. Various modifiers are available to customize the appearance and text alignment. Actions can be performed when editing begins and ends for the text field or when the return key is pressed.

Stepper control

The stepper control increments and decrements a value. A closed range can be used to limit the applicable stepper values.

Sidebar navigation

A navigation view contains a master and detail view. In this example, the master view displays a list of fruits in the sidebar. The detail view determines which view to display based on which item is selected in the sidebar.

Preferences window

A preferences window contains user settings for the application. This window is usually opened from the Preferences item in the application’s menu bar. To open a window from the Preferences menu item, connect an IBAction between the item and AppDelegate.

Picker control

The picker control selects an item from a set of values. The appearance of the picker can be changed by using different styles and modifiers.








Gavin Wiggins © 2020