Search code examples
swiftmacosnsviewnsviewcontroller

How make custom view and over it's NSButton is clickable in macOS swift?


I've NSButton in NSView. The NSView has NSClickGestureRecognizer. Now I am able to get action from NSView that have NSClickGestureRecognizer. But the problem is NSButton is not able click.

How can I solve this problem?

Code:

import Cocoa

class ViewController: NSViewController {

    @IBAction func actionGesture(_ sender: NSClickGestureRecognizer) {

        print("Gesture Clicked!")
    }

    @IBAction func actionButton(_ sender: NSButton) {
             print("Button Clicked!")
    }

}

Screenshot:

enter image description here

Project Link: https://drive.google.com/open?id=1HmbKiqdF9z7SH131IyyfJ21j8KOgabzl


Solution

  • The easyest way to solve this is to arrange the View structure like showen below. So the View connected to the NSClickGestureRecognizer is behind the button.

    enter image description here

    Hope this helps