user11145365
user11145365

Reputation: 313

Right click on UIButton for UIKitForMac (catalyst)

Is it possible to easily enable right click on a Mac Catalyst app within UIKitForMac?

Currently the following code works perfectly on left click, but nothing is called on right click:

button.addTarget(self, action: #selector(doSomething), for: .touchUpInside)

// Is called for left click but not for right click

@objc func doSomething(sender:UIButton, event:UIEvent) -> Bool {

Upvotes: 0

Views: 707

Answers (1)

neskafesha
neskafesha

Reputation: 184

https://developer.apple.com/design/human-interface-guidelines/ios/controls/context-menus/

This menu works by right clicking on Mac Catalyst

1. Add Iteraction

let interaction = UIContextMenuInteraction(delegate: self)
 yourButton.addInteraction(interaction)

2. Add extension UIContextMenuIteractionDelegate

extension AccountViewVC: UIContextMenuInteractionDelegate {

 public func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
      
      return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
           let delete = UIAction(title: "Delete", image: UIImage(systemName: "arrow")) { action in
                
           }
           let children = [delete]
           return UIMenu(title: "Main Menu", children: children)
      })
 }}
 

Upvotes: 1

Related Questions