Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSButton white background when clicked

When creating Cocoa bevel button with custom image and alternate image I'm having a strange behavior. In the pressed state the button background becomes white. I'm adding the button as subview of a transparent window (HUD window).

I'm trying every technique that I know:

NSButton *closeButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 30.0, 30.0)];
        [closeButton setFrameOrigin:NSMakePoint(0.0, 0.0)];
        [closeButton setImagePosition:NSImageOnly];
        [closeButton setAction:@selector(closeWindowAction:)];
        [closeButton setBordered:NO];
        [closeButton setTransparent:NO];

        [closeButton setImage:[NSImage imageNamed:@"icon-tclose-off"]];
        [closeButton setAlternateImage:[NSImage imageNamed:@"icon-tclose-on"]];
        [closeButton setBezelStyle:NSShadowlessSquareBezelStyle];
        [closeButton setButtonType:NSMomentaryLightButton];

        //[[closeButton cell] setBackgroundColor:[NSColor clearColor]];
        [[closeButton cell] setHighlightsBy:NSChangeBackgroundCellMask|NSCellLightsByContents];
        //[[closeButton cell] setHighlightsBy:NSContentsCellMask];
        //[[closeButton cell] setShowsStateBy:0|NSContentsCellMask];

I also tried

[closeButton setButtonType:NSMomentaryChangeButton];

[[closeButton cell] setHighlightsBy:NSContentsCellMask];

with no results.

You can see the wrong behavior in the attached screenshots:

Bevel button overlaying a HUD window:
Bevel button overlaying a HUD window

Wrong bevel button background:
Wrong Bevel button background

like image 631
loretoparisi Avatar asked Oct 13 '11 15:10

loretoparisi


2 Answers

Depending on your situation, this may also work:

Change the style of the button to Bevel or Square, the mode should be set to "Momentary Change" and Border, Transparent, Mixed and Selected should be OFF. This is how I fixed the white background problem on my buttons.

like image 162
Ryan Avatar answered Oct 19 '22 07:10

Ryan


I have made it work by setting cell.highlightsBy to ContentsCellMask:

let btn = NSButton(frame: myFrame)

btn.image = myButtonImage
btn.image?.size = myFrame.size
btn.imagePosition = .ImageOnly

btn.bordered = false      
(btn.cell as? NSButtonCell)?.highlightsBy = .ContentsCellMask

view.addSubview(btn)

This way the button is darkened when pressed, but no ugly square appears. Tested only in El Capitan).

like image 42
Georgy Pashkov Avatar answered Oct 19 '22 06:10

Georgy Pashkov