How to change NSToolBar background color?

我们两清 提交于 2021-02-08 03:56:12

问题


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?


回答1:


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.

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

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.

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.

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.

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



来源:https://stackoverflow.com/questions/26371550/how-to-change-nstoolbar-background-color

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