Frage

I want to draw image background in NSView and place some elements which also have transparent background on top of it.

I've overrided draw rect of NSView:

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

NSImage *image = [NSImage imageNamed:@"header_background.gif"];

[image drawInRect:CGRectMake(0, rect.size.height - 75, rect.size.width, 75) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

[NSGraphicsContext restoreGraphicsState];
}

NSWindow init:

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:aStyle backing:bufferingType defer:flag];
if (self) {
    [self setOpaque:NO];
    [self setHasShadow:YES];
    [self setBackgroundColor:[NSColor clearColor]];
}

return self;
}

After that a've got such weird effect. Backgrounds of Sliders and buttons are also blinking sometimes enter image description here

What am I doing wrong?

War es hilfreich?

Lösung

Well, first off, your “color” is ignored when drawing images, so that’s not doing anything. Second, the code I see here probably isn’t causing the problem, can you post the rest of the file?

Right now I’m suspecting maybe you have marked this view as being opaque, when it’s not, or that you have something else odd in your view hierarchy.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top