[英]Setting background color in drawRect of NSSearchField subclass
我有一個NSSearchField
的自定義子類,我想設置其背景色。
@interface CustomNSSearchField : NSSearchField
@end
到目前為止,我已經嘗試過:
嘗試#1
@implementation CustomNSSearchField
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
[self setDrawsBackground:YES];
[self setBackgroundColor:[NSColor redColor]];
}
完全沒有視覺變化:
嘗試#2
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
[[NSColor redColor] setFill];
NSRectFill(rect);
}
結果是:
如何設置的范圍內 ,搜索領域的文本后面的背景顏色?
您必須重新繪制整個內容。 沒有屬性來專門更改NSSearchField的背景顏色。 看看這個例子:
編輯:
還有值得指出的地方。 您永遠不應覆蓋控件的drawRect方法。 您應該創建NSSearchFieldCell的子類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.