NSButtonCell hover image, transparent background on click?

I implemented NSButton with the image in it.

When the user freezes, the image changes to something else, and then back.


Normal status:

enter image description here

On hover:

enter image description here


The code I use for NSButtonCellis:

Interface:

#import <Foundation/Foundation.h>

@interface DKHoverButtonCell : NSButtonCell 
{
    NSImage *_oldImage;
    NSImage *hoverImage;
}

@property (retain) NSImage *hoverImage;

@end

Implementation:

#import "DKHoverButtonCell.h"

@interface NSButtonCell()
- (void)_updateMouseTracking;
@end

@implementation DKHoverButtonCell

@synthesize hoverImage;

- (void)mouseEntered:(NSEvent *)event {
    if (hoverImage != nil && [hoverImage isValid]) {
        _oldImage = [[(NSButton *)[self controlView] image] retain];
        [(NSButton *)[self controlView] setImage:hoverImage];
    }
}

- (void)mouseExited:(NSEvent *)event {
    if (_oldImage != nil && [_oldImage isValid]) {
        [(NSButton *)[self controlView] setImage:_oldImage];
        [_oldImage release];
        _oldImage = nil;
    }
}

- (void)_updateMouseTracking {
    [super _updateMouseTracking];
    if ([self controlView] != nil && [[self controlView] respondsToSelector:@selector(_setMouseTrackingForCell:)]) {
        [[self controlView] performSelector:@selector(_setMouseTrackingForCell:) withObject:self];
    }
}

- (void)setHoverImage:(NSImage *)newImage {
    [newImage retain];
    [hoverImage release];
    hoverImage = newImage;
    [[self controlView] setNeedsDisplay:YES];
}

- (void)dealloc {
    [_oldImage release];
    [hoverImage release];
    [super dealloc];
}

@end

Now, here is the problem:

  • although the controls described above work at 100% (with a rounded “X” image and a transparent background), when the user clicks on it, it displays a “white” background and does not save my old “transparent” background
  • How do I solve this?
+3
source share
2 answers

. :

how to do it

:

[button setButtonType:NSMomentaryChangeButton];
+8

showsBorderOnlyWhileMouseInside NSButtonCell, (OS X 10.0). , . , -, , .

API ( _updateMouseTracking).

closeButton.bezelStyle = .inline
closeButton.setButtonType(.momentaryPushIn)

if let buttonCell = closeButton.cell as? NSButtonCell {
    buttonCell.showsBorderOnlyWhileMouseInside = true
}
0

All Articles