4

I want to disable the user interaction of NSButton. The only possible option seems to be setting isEnabled to false:

button.isEnabled = false

...but the problem is that it's giving a white shade to the image of my button. Is there any way to get rid of that shade?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
bisma
  • 795
  • 6
  • 26

3 Answers3

8

You have to set the imageDimsWhenDisabled property of the corresponding button cell:

button.isEnabled = false
if let cell = button.cell as? NSButtonCell {
    cell.imageDimsWhenDisabled = false
}
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

One option would be instead of disabling it you can let it fire and handle it accordingly in code. such as do nothing when condition to disable applies.

XP.
  • 52
  • 7
  • There is more than one button overlapping. I need to disable one type of buttons otherwise it will eat the touch event of the button behind. Can't do that. – bisma Apr 20 '18 at 16:53
0

As this answer says, override NSButton and return nil for hitTest(_:).

If you want to toggle whether user interaction is enabled, create a custom variable and return super.hitTest(point) if you want the button to be enabled.

class CustomButton: NSButton {
    var isUserInteractionEnabled = true

    override func hitTest(_ point: NSPoint) -> NSView? {
        return isUserInteractionEnabled ? super.hitTest(point) : nil
    }
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223