# Creating a macOS Action (Gear) Button Programmatically in Swift

DevFeed: [Creating a macOS Action (Gear) Button Programmatically in Swift](<https://devfeed.tech/articles/creating-a-macos-action-gear-button-programmatically-in-swift-22317.md>)

Original publisher: [Read original article](<https://www.thecodedself.com/macOS-action-button-swift/>)

Author: Keegan Rush

Published: 2018-07-17T00:00:00Z

Content type: tutorial

Language: en

Sources: [The Coded Self](<https://devfeed.tech/sources/the-coded-self.md>)

Topics: [macOS](<https://devfeed.tech/topics/macos.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [Code](<https://devfeed.tech/topics/code.md>), [ui](<https://devfeed.tech/topics/ui.md>)

Tags: [app](<https://devfeed.tech/tags/app.md>), [code](<https://devfeed.tech/tags/code.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [macos](<https://devfeed.tech/tags/macos.md>), [swift](<https://devfeed.tech/tags/swift.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

A tutorial on creating a macOS action button programmatically in Swift. It explains how to use an NSPopupButton, position it, add menu items, add the system gear icon, and configure its button cell styling.

## Source excerpt

For a list of actions on macOS, the standard control is an action button as defined in the macOS Human Interface Guidelines. Here's the definition from that page: An action button (often referred to as an action menu) is a specific type of pull-down button that operates like a contextual menu, without the disadvantage of being hidden, providing access to app-wide or table-specific commands. An action button includes a gear icon when closed and a downward arrow indicator that alludes to its menu. Action buttons are often used in toolbars, but can also be used in the content area of a view beneath a table view. Apple isn't very clear about how to create one of these in code. They tell you to create a button using the system-provided gear icon by making use of the NSImageNameActionTemplate image name, but try as I might, I couldn't replicate the action button used in Finder from that image name alone. It turns out that there are a few more steps involved. Create an NSPopupButton let actionButton = NSPopUpButton(frame: .zero, pullsDown: true) It's clear that the button is an NSPopupButton due to the dropdown that it presents. Position the button view.addSubview(actionButton) actionButton.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ actionButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), actionButton.centerYAnchor.constraint(equalTo: view.centerYAnchor) ]) Position the button where you need it. For the purposes of this demo, I'm leaving it right in the middle of the parent view. For the reasoning behind actionButton.translatesAutoresizingMaskIntoConstraints = false, take a look at my post on autoresizing masks. We've created the button and positioned it in the view. This is the result: It's coming together! Add some items ["Option 1", "Option 2", "Option 3"].forEach(actionButton.addItem) An action button isn't too useful without some actions. With an array of item titles, we use the forEach operator to add the items to the a