1

I'm a iOS developer, and recently I'm programming a desktop APP for MAC OSX. I still don't have much experience with the View's components of OSX, so maybe it's a silly or easy question, but I have made a little research about this problem and haven't found any solution yet.

Here's the problem:

I have a custom specialization of a NSView, that is used as the view of a Content ViewController used in my NSPopover.

Inside this view, that I'm calling "PopoverBackgroundView", I painted inside the drawRect this red background, and calculated another minor rect and painted with this gray-like color. Here's the code:

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor colorWithDeviceRed:174/255.0 green:72/255.0 blue:72/255.0 alpha:1.0] setFill];
    NSRectFill(dirtyRect);

    [[NSColor colorWithDeviceRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0] setFill];
    NSRectFill(NSMakeRect(BORDER_WIDTH, BORDER_WIDTH, dirtyRect.size.width - 2*BORDER_WIDTH, dirtyRect.size.height - 2*BORDER_WIDTH));
}

So, inside the PopoverBackgroundView.m I'm programatically creating a NSComboBox. This comboBox will have the numbers 1 to 10. When I allocate it, everything seems just fine:

enter image description here

The problem is, after I select any options inside the combobox, it's background somehow "goes away" became transparent, I don't know, and become like this:

enter image description here

Please notice the red-like frame (background color of the view) around the NSComboBox, that appeared just AFTER I select something.

Here's the code where I'm allocation the comboBox and initializing it:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

         (...)

        self.comboBox = [[NSComboBox alloc] initWithFrame:CGRectMake(15, frame.size.height - 55, 90, 25)];
        self.comboBox.delegate = self;
        [self.comboBox setDrawsBackground:NO];
        [self.comboBox setSelectable:YES];
        [self.comboBox setEditable:NO];

        for (int i = 1; i<=10; i++)
        {
            NSString *mystr = [NSString stringWithFormat:@"%d", i];
            [self.comboBox addItemWithObjectValue:mystr];
        }

        [self addSubview:self.comboBox];

    }

    return self;
}

Any idea how can I 'fix' this "selected background"? All that I want it's the selected state to be equals to the normal state, i. e. ,the comboBox should be always like the first image, even after the selection.

Is there something wrong with the allocation code? Something mission? I'm really thinking that just some property that I'm not using or initializing, but I couldn't find yet.

Thanks in advance,

4

1 回答 1

2

只是为了提供反馈,我终于能够解决我的问题。

我不知道确切原因,但问题都是因为我在drawRect. 不知何故,这些代码

[[NSColor colorWithDeviceRed:174/255.0 green:72/255.0 blue:72/255.0 alpha:1.0] setFill];
NSRectFill(dirtyRect);

被子视图传播,不知道是setFill还是NSRectFill。因此,NSCombobox 的“背景”被涂上了这种颜色。

看到这篇文章后:在 NSView 中添加边框和圆角矩形后, 我将绘制矩形更改为:

- (void)drawRect:(NSRect)dirtyRect
{
    NSBezierPath *background = [NSBezierPath bezierPathWithRoundedRect:self.bounds xRadius:10 yRadius:10];
    [[NSColor colorWithDeviceRed:174/255.0 green:72/255.0 blue:72/255.0 alpha:1.0] set];
    [background setLineWidth:10];
    [background stroke];
}

现在一切正常,正如我所愿。现在,在我选择我的组合框后,没有画出奇怪的背景。

在此处输入图像描述

如果有人知道以前的代码为什么会发生这种情况,请告诉我。

于 2013-08-06T18:52:08.120 回答