当窗口具有焦点时更改 NSView 背景颜色
我注意到,当应用程序窗口包含大纲视图(例如 XCode)时,当该窗口处于焦点状态时,它会改变颜色。以 XCode 为例,如果窗口是当前窗口,则大纲视图具有蓝色背景,如果失去焦点,它会变成灰色,
任何人都可以帮助我复制这个吗?我认为它与 drawRect:
有关,但只能设法在窗口加载时改变颜色。
也许它是一个内置函数,而我只是错过了一些东西?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
-drawRect:
中所需要做的就是检查窗口是否具有 main 状态并进行相应的绘制:All you have to do in your
-drawRect:
is check whether the window has main status and draw accordingly:每当窗口获取或放弃主窗口或关键窗口状态时,窗口的委托都会收到消息。您可以在窗口委托中实现适当的方法(例如
-windowDidBecomeMain:
和-windowDidResignMain:
),以根据需要更新窗口及其子视图。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.