2
votes

I'm trying to draw some things on the background of my windows. Therefore I subclassed the NSView of the window and added some drawing code like this:

- (void)drawRect:(NSRect)dirtyRect {
    float color = 0.95;
    [[NSColor colorWithDeviceRed:color green:color blue:color alpha:1.0] set];
    NSRectFill(NSMakeRect(320, 0, 220, NSHeight(dirtyRect)-60));
}

This works great, but as soon as I open a NSComboBox or if I activate a checkbox, the background of these elements erases my just drawn rect.

I don't understand this, because checking for example the checkbox causes, that drawRect is called (I added a NSLog). Only resizing the window draws my rect again.

EDIT: here is a screenshot of the problem:

enter image description here

1
call [view setNeedsDisplay] after the other UI elements are dismissed and you need to redraw the backgroundJack
Hi, thanks for your answer. But this "solution" is insane. It causes a very ugly flickering and it's very inconvenient, because I have a) create for every element an IBAction and b) I have to put this line into this action.Lupurus
Can you post some images of your exact problem and maybe we can find a better solutionJack
What relation do the coordinates in your NSRectFill() call have to your view's bounds? Why are they not in fact computed from the bounds (e.g. with offsets or whatever)?Ken Thomases

1 Answers

2
votes

I sometimes face the same problem. I think the following is what I use.

/// .h
@interface BackgroundView1 : NSView {
    NSImage *myImage;
}

// .m
- (void)awakeFromNib {
    [self setupBackgroundImage];
}

- (void)setupBackgroundImage {
    NSColor *c = [NSColor colorWithDeviceRed:0.0f/255.0f green:55.0f/255.0f blue:150.0f/255.0f alpha:1.0f];
    if (myImage == nil)
        myImage = [self createColorImage:NSMakeSize(1,1):c];
}

- (void)drawRect:(NSRect)rect { 
    [myImage drawInRect:NSMakeRect([self bounds].origin.x,[self bounds].origin.y,[self frame].size.width,[self frame].size.height)
               fromRect:NSMakeRect(0,0,[myImage size].width, [myImage size].height)
              operation:NSCompositeCopy
               fraction:1.0];
}

// Start Functions //
- (NSImage *)createColorImage:(NSSize)size :(NSColor *)color {
    NSImage *image = [[NSImage alloc] initWithSize:size];
    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
                             initWithBitmapDataPlanes:NULL
                             pixelsWide:size.width
                             pixelsHigh:size.height
                             bitsPerSample:8
                             samplesPerPixel:4
                             hasAlpha:YES
                             isPlanar:NO
                             colorSpaceName:NSCalibratedRGBColorSpace
                             bytesPerRow:0
                             bitsPerPixel:0];

    [image addRepresentation:rep];
    [image lockFocus]; // Lock focus of image, making it a destination for drawing
    [color set];
    NSRectFill(NSMakeRect(0,0,size.width,size.height));
    [image unlockFocus];
    return image;
}
// End Functions //