Question

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,

Was it helpful?

Solution

Just to give a feedback, I finally was able to resolve my problem.

I don't know exactly why, but the problem was all caused because the way I was drawing my border in the drawRect. Somehow, these code

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

were been propagated by the subviews, don't know if was the setFill or the NSRectFill. So, the "background" of the NSCombobox was been painted with this color.

After seeing this post :Adding border and Rounded Rect in the NSView I changed my draw rect to this:

- (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];
}

and everything is working fine now, as I wanted. Now, after I select my combobox, no strange background is been drawing.

enter image description here

If someone knows whey this was happening with the previous code, please, let me know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top