简体   繁体   中英

Avoiding making a NSButton transparent / see-through when isEnabled is false (disabled)

A regular NSButton seems to be transparent if it's disabled.

在此处输入图片说明

Image shows a Button with style 'push'

However, I want to disable the Button without the transparency.

I've tried to programmatically set the alphaValue to 1.0 but it seems that the NSButtons cell (controlView) as well as all other subviews are already at an alphaValue of 1.0.

Also, there's nothing like userInteractionEnabled or adjustsImageWhenDisabled (both recommended here ) I could use like in iOS.

How can I disable a NSButton without the standard transparency?

EDIT : Also a 'textured push' button appears to be transparent: 在此处输入图片说明

If you don't need the title, and will provide your own button image, you can use setImageDimsWhenDisabled to disable transparency in NSButtonCell 's image when it is disabled. Here is the code:

[buttonCell setImageDimsWhenDisabled:NO];

NSButtonCell is the subview of NSButton . But as I said, the title will still be "dimmed" a little bit, and also its background. But since image if on top of the background, you won't see the transparent background.

Another way (avoid subclass NSButton) is to have your own state tracking variable for button. When the state is disabled, dismiss any click event. Sample framework of code is like this:

- (IBAction)clickOnButton {
    static BOOL isEnabled = YES;
    if (isEnabled) {
        // Handle click event here...

    }
    isEnabled = !isEnabled;
}

This may show the highlight when clicked. You can disable highlight also. But this is not a good idea if you have many buttons. If you have many buttons, subclass NSButton is the best choice.

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