Skip to content

Instantly share code, notes, and snippets.

@TheCodedSelf
Last active July 17, 2018 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheCodedSelf/e07a789cb6319cc8cd70a21cb5ce0989 to your computer and use it in GitHub Desktop.
Save TheCodedSelf/e07a789cb6319cc8cd70a21cb5ce0989 to your computer and use it in GitHub Desktop.
Create a macOS Action (Gear) Button Programmatically (www.thecodedself.com/macOS-action-button-swift/)
let actionButton = NSPopUpButton(frame: .zero, pullsDown: true)
view.addSubview(actionButton)
actionButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
actionButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
actionButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
["Option 1", "Option 2", "Option 3"].forEach(actionButton.addItem)
let actionItem = NSMenuItem()
actionItem.image = NSImage(named: .actionTemplate)
actionButton.menu?.insertItem(actionItem, at: 0)
let cell = actionButton.cell as? NSButtonCell
cell?.imagePosition = .imageOnly
cell?.bezelStyle = .texturedRounded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment