How to NOT highlight the NSButton's template image when clicked?

跟風遠走 提交于 2019-12-23 07:48:42

问题


I have NSButtons in each row of a NSTableView.

The buttons images are set in IB and are black icons with alpha channel:

The windows are set to dark mode with:

window?.appearance = NSAppearance(named: NSAppearanceNameVibrantDark)

And to normal mode with:

window?.appearance = nil

The goal is that the buttons should be black when the window is white, and should be white when the window is dark, without changing when clicked.

In dark mode, in order to achieve the color change, I set the button's image "template" property to true in the NSTableCellView subclass:

public override func awakeFromNib() {
    if darkMode {
        button.image?.isTemplate = true
    } else {
        button.image?.isTemplate = false
    }
} 

The buttons are made like this in IB:

This works well in macOS 10.12 Sierra: when the window is white, clicking on the black button does not change its appearance and this is perfect.

But when the window is dark, clicking on the white button makes it black, which is not acceptable in my case (*). Also, the template image just doesn't work in macOS 10.11 El Capitan.

Ok:

Clicked, not ok:

And clicking again on the blackened button makes it white again...

My question is: how to force the NSButton to not highlight the template image when clicked?

I thought this was because of the "Momentary Change" type but using the other "momentary" ones doesn't change anything, and I can't set the type to "custom" either (IB refuses).

Please note that these template images behave differently in macOS 10.11 compared to macOS 10.12. The combinations of settings that work seamlessly in Sierra don't work in El Capitan. This is also why I've set a bounty: I need a solution for both systems. If there's no solution and you know why, then your explanation would also be an acceptable answer.

(*) a workaround is available on Sierra, where the template image works, by using use button.cell?.setCellAttribute(.cellLightsByContents, to: 0) to stop the button from changing color definitely when clicked (there's still a short flash though). But in El Capitan the button is black in dark mode, so this doesn't apply.

来源:https://stackoverflow.com/questions/44112814/how-to-not-highlight-the-nsbuttons-template-image-when-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!