【发布时间】:2017-06-23 20:53:44
【问题描述】:
我有一个带有图像的 NSButton。当推动整个细胞变成灰色。如何防止这种情况? 关于这个主题有几篇文章。但他们中的大多数人都像 10 岁。最近的一个在这里:NSButton background transparent after getting focus
据此,我尝试了以下代码:
class overviewImageButton: NSButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
convenience init(appearance: NSAppearance) {
self.init(appearance: appearance)
self.appearance = NSAppearance(named: NSAppearanceNameAqua)
}
override func draw(_ dirtyRect: NSRect) {
self.image = NSImage(named: "buttonImage.png")
super.draw(dirtyRect)
NotificationCenter.default.addObserver(forName: windowChanged, object: nil, queue: nil) {
notification in
self.image = NSImage(named: "buttonImage_highlighted.png")
}
}
}
但它不起作用。按下按钮时,纽扣电池仍会变为灰色。感谢您的帮助!
【问题讨论】:
-
不,这不是
Possible duplicate。这个问题解决了。这是一个新话题。 -
答案是一样的。只画
draw。设置init中的属性。 -
不要在
draw中设置图片,也不要调用super两次。按钮的样式和类型是什么?还是您在代码中创建按钮? -
什么是
optionsButton?
标签: swift cocoa colors nsbutton appearance