简体   繁体   中英

Disable NSButton without grayed image

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?

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
}

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.

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM