Cocoa 单击按钮时显示白色背景

Cocoa 单击按钮时显示白色背景,cocoa,nsbutton,nsbuttoncell,Cocoa,Nsbutton,Nsbuttoncell,当创建带有自定义图像和备用图像的Cocoa斜面按钮时,我有一个奇怪的行为。在按下状态下,按钮背景变为白色。 我将添加按钮作为透明窗口(HUD窗口)的子视图 我正在尝试我所知道的每一种技巧: NSButton *closeButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 30.0, 30.0)]; [closeButton setFrameOrigin:NSMakePoint(0.0, 0.0)];

当创建带有自定义图像和备用图像的Cocoa斜面按钮时,我有一个奇怪的行为。在按下状态下,按钮背景变为白色。 我将添加按钮作为透明窗口(HUD窗口)的子视图

我正在尝试我所知道的每一种技巧:

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];
我也试过了

[closeButton setButtonType:NSMomentaryChangeButton];

[[closeButton cell] setHighlightsBy:NSContentsCellMask];
没有结果

您可以在附加的屏幕截图中看到错误的行为:

覆盖HUD窗口的斜面按钮:

斜角按钮背景错误:
创建按钮

NSButton *myButton;
myButton = [[NSButton new] autorelease];
[myButton setTitle: @"Hello!"];
[myButton sizeToFit];
[myButton setTarget: self];
[myButton setAction: @selector (function:)];
将按钮添加到窗口

unsigned int styleMask = NSTitledWindowMask 
                           | NSMiniaturizableWindowMask;
NSWindow *myWindow;
myWindow = [NSWindow alloc];
/*get the size of the button*/
NSSize buttonSize;
buttonSize = [myButton frame].size;
/*set window content rect with the size of the button, and with an origin of our choice; (100, 100)*/
NSRect rect;
rect = NSMakeRect (100, 100, 
                   buttonSize.width, 
                   buttonSize.height);

myWindow = [myWindow initWithContentRect: rect
                       styleMask: styleMask
                       backing: NSBackingStoreBuffered
                       defer: NO];
[myWindow setTitle: @"my window"];
/*replacing the default window content view with our button*/
[myWindow setContentView: myButton];

根据您的情况,这可能也适用于:


将按钮的样式更改为斜角或方形,模式应设置为“瞬时更改”,边框、透明、混合和选定应关闭。这就是我修复按钮白色背景问题的方法。

您应该设置按钮类型:
myButton.buttonType=NSInstantilyChangeButton

我通过将
cell.highlightsBy
设置为
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)

这样按下按钮时按钮会变暗,但不会出现难看的方块。仅在El Capitan测试)。

我不清楚这是哪个NSWindow myWindow。是按钮容器吗?在本例中,它的content rect就是您定义的NSRect rect?我尝试了此解决方案,并替换了窗口的contentView修复了NSButton的白色背景问题,但它引入了另一个问题,即关键窗口焦点。在一种情况下会发生这种情况:单击关闭按钮但不释放它(因此只执行按钮按下事件)。在这种情况下,处理按钮的NSWindow获得焦点,下面的窗口(以及其他窗口)将失去焦点。因此,问题是用户必须在另一个窗口上单击两次(在我的例子中是HUD)在获得焦点之前。您还可以添加
btn.alternateImage=MyButtonInternateImage
,并在按下按钮时显示。这在Mac OS 10.12上是一个问题,但在10.13上不是。