我想根据窗口是否获得焦点来更改 NSView 的背景。
我发现了这个问题:Change NSView background color when window has focus
第一个答案工作正常,只是当窗口失去或获得焦点时不会触发重绘。
如何在主窗口发生变化时触发重绘?
最佳答案
在您的 NSWindow
委托(delegate)中:
func windowDidBecomeMain(notification: NSNotification) {
let theView = ...
theView.needsDisplay = true
}
func windowDidResignMain(notification: NSNotification) {
let theView = ...
theView.needsDisplay = true
}
在您的 NSView
子类中:
func drawRect(rect: NSRect) {
if self.window.isMainWindow {
// draw active appearance
} else {
// draw inactive appearance
}
}
关于cocoa - 当 mainWindow 改变时重绘 NSView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32293592/