如何更改NSPopupButton的背景颜色? [英] How to change the background color of an NSPopupButton?
问题描述
我正在尝试解决一个听起来很简单的问题:更改NSPopupButton的背景颜色.
I am trying to tackle a problem which sounds pretty simple: changing the background color of an NSPopupButton.
Interface Builder仅允许将样式更改为预定义的样式,而不允许更改背景颜色.同样,设置 IBOutlet
也无济于事,因为 NSPopupButton
没有 setBackgroundColor
方法.
Interface Builder only allows changing the style to a pre-defined one and doesn't allow changing the background color. Also, setting up an IBOutlet
didn't help since NSPopupButton
doesn't have a setBackgroundColor
method.
我也尝试将 NSPopupButton
子类化以覆盖 drawRect
方法.这是我尝试过的:
I also tried subclassing NSPopupButton
to override the drawRect
method. Here's what I have tried:
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor redColor] setFill];
NSRectFill(dirtyRect);
}
这会在 NSPopupButton
上绘制一个红色矩形,而不是将其设置为背景色.
This draws a red rectangle over the NSPopupButton
rather than setting it as a background color.
关于如何解决这个问题的任何想法?
Any ideas on how to go about solving this?
推荐答案
您应该创建NSPopUpButtonCell的子类,然后覆盖
You should create a subclass of NSPopUpButtonCell, then override
- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
NSPopupButtonCell is a subclass of NSButtonCell which defines several methods for drawing individual cell components, eg bezel, title, image.
然后,您可以展开NSPopupButton并将其单元格子类更改为新的子类,它应该使用您的绘制方法.
You can then expand the NSPopupButton and change its cell subclass to your new subclass and it should use your drawing methods.
与iOS不同,可可主要使用NSCell来处理绘图
Cocoa primarily uses NSCell to handle drawing, unlike iOS
这篇关于如何更改NSPopupButton的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!