'How to disable Click through to a NSView behind another NSView
I have two NSViews on top of each other. One NSView presents a table with rows. On clicking a row another view is shown on top of that view.
Now the problem is when I click on an area on the second view where there is a row on the underneath NSView then it gets clicked. How can I stop that?
Thanks
Solution 1:[1]
On your top level view, implement mouseDown:
in that view and do not call nextResponder or do anything in it.
- (void)mouseDown:(NSEvent *)theEvent{
//Do nothing to not propagate the click event to descendant views
}
I have the exact same scenario as the OP and this is working for me as intended. Credit to the answer I found here that led me to this.
Solution 2:[2]
Subclass and implement acceptsFirstMouse
in the view, returning YES
. Also, set acceptsTouchEvents
to YES
in that view.
import Cocoa
class UnView: NSView{
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
return false
}
override var allowedTouchTypes: NSTouch.TouchTypeMask {
get { return [] }
set { }
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | zic10 |
Solution 2 | Fattie |