如何更改NSToolBar的背景颜色? [英] How to change NSToolBar background color?

查看:102
本文介绍了如何更改NSToolBar的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NSToolbar 并尝试将颜色更改为白色而不是默认的灰色.我该怎么办?

I'm using a NSToolbar and I'm trying to change the color to a white instead of the default gray. How can I accomplish that?

推荐答案

由于工具栏已集成到窗口标题栏中,因此您需要同时绘制标题栏工具栏.这基本上意味着您需要接管整个窗口的绘图.

Since the toolbar integrates into the window title bar, you need to draw both the title bar and the toolbar. Which basically means you need to take over drawing for the entire window.

这需要旋转"尽管运行良好,但Apple不支持该功能,因此可能会导致您的应用被应用商店拒绝.

This requires "swizzling" which, while it works well, is not supported by Apple and therefore may get your app rejected from the app store.

基本上,您需要通过使用Objective-C运行时函数来修改类定义,从而将自己的代码注入NSWindow类.请注意,这会影响您应用中的所有窗口,因此您需要使用if语句来检查这是否是您要修改的窗口.

Basically you need to inject your own code into the NSWindow class, by using objective-c runtime functions to modify the class definition. Note that this will affect all windows in your app, so you will need to have an if statement checking if this is a window you want to modify.

子类NSWindow,并在 + initialize 方法中,找到框架视图"(frame view).该类是完成大部分窗口绘制的类,并将其重命名为"drawRect:"."originalDrawRect:"方法.然后,您定义一个新的"drawRect:".类上的method,作为NSWindow子类中方法的副本.此方法应首先调用originalDrawRect,然后在其之上进行自定义绘制.

Subclass NSWindow, and in the +initialize method, find the "frame view" class which is the one that does most of the window drawing, and rename it's "drawRect:" method to "originalDrawRect:". Then you define a new "drawRect:" method on the class, as a duplicate of a method in your NSWindow subclass. This method should first call originalDrawRect and then do custom drawing on top of it.

注意:您将在窗口标题文本的顶部绘图...因此需要将绘图模式更改为 kCGBlendModeColor 或其他内容.否则,只需再次绘制标题字符串即可.您可以询问窗口标题文字的绘制位置.

Note: you will be drawing over the top of the window title text... so'll need to change the the drawing mode to kCGBlendModeColor or something. Or else just draw the title string again. You can ask the window where the title text should be drawn.

这里是全文,其中包含更多详细信息: http://parmanoir.com/Custom_NSThemeFrame

Here's a full article with more details: http://parmanoir.com/Custom_NSThemeFrame

这篇关于如何更改NSToolBar的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆