问题
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