Change NSView background color when window has focus

ε祈祈猫儿з 提交于 2019-12-24 00:39:51

问题


I have noticed when an apps window contains an Outline view (such as XCode) it changes color when that window is in focus. With XCode for example, if the window is current then the outline view has a blueish background, if it looses focus it goes grey,

Can anyone help me to replicate this? I presume its something to do with drawRect: but can only manage to get the color to change when the window loads.

Maybe its a built in function and I'm just missing something?


回答1:


All you have to do in your -drawRect: is check whether the window has main status and draw accordingly:

- (void)drawRect:(NSRect)rect
{
    if ([[self window] isMainWindow]) {
        // draw active appearance
    } else {
        // draw inactive appearance
    }
}



回答2:


A window's delegate gets messages whenever a window gets or resigns main or key window status. You can implement the appropriate methods (like -windowDidBecomeMain: and -windowDidResignMain:) in your window delegate to update the window and its subviews as necessary.



来源:https://stackoverflow.com/questions/6710134/change-nsview-background-color-when-window-has-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!