问题
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