mainWindow更改时重绘NSView

时间:2015-08-30 05:46:03

标签: cocoa swift2 appkit redraw

我想根据窗口是否聚焦来改变NSView的背景。

我发现了这个问题:Change NSView background color when window has focus

第一个答案正常,但窗口失去或获得焦点时不会触发重绘。

如何在mainWindow更改时触发重绘?

2 个答案:

答案 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的参数来监视windowobject