我想根据窗口是否聚焦来改变NSView的背景。
我发现了这个问题:Change NSView background color when window has focus
第一个答案正常,但窗口失去或获得焦点时不会触发重绘。
如何在mainWindow更改时触发重绘?
答案 0 :(得分:2)
在NSWindow
代表中:
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
}
}
答案 1 :(得分:0)
使用NSNotificationCenter的addObserver:selector:name:object:
或addObserverForName:object:queue:usingBlock:
以视图的NSWindowDidBecomeMainNotification
作为NSWindowDidResignMainNotification
的参数来监视window
和object
。