簡體   English   中英

在NSSearchField子類的drawRect中設置背景色

[英]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]];
}

完全沒有視覺變化:

嘗試1

然后,我按照這里的建議進行嘗試:

嘗試#2

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];

    [[NSColor redColor] setFill];
    NSRectFill(rect);
}

結果是:

嘗試2

如何設置的范圍 ,搜索領域的文本后面的背景顏色?

您必須重新繪制整個內容。 沒有屬性來專門更改NSSearchField的背景顏色。 看看這個例子:

自定義NSSearchField

編輯:

還有值得指出的地方。 您永遠不應覆蓋控件的drawRect方法。 您應該創建NSSearchFieldCell的子類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM