0

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?

Denis Kildishev
  • 709
  • 2
  • 7
  • 15

1 Answers1

1

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.

Wil Shipley
  • 9,343
  • 35
  • 59
  • Thank you for your response. I have updated first post to the current condition. The problem still here.. By the way, when i'm resizing window background of elements becomes normal (not transparent). But when they are being updated, problem appears again. – Denis Kildishev Jan 13 '14 at 00:19
  • Why are you calling setOpaque:NO on the window? Are you trying to make a window that’s totally clear? What about the titlebar? – Wil Shipley Jan 13 '14 at 00:23
  • Yes, I'm going to make window totally clear later, because I want to make my own titlebar. Now titlebar is shown only for debugging. – Denis Kildishev Jan 13 '14 at 00:27
  • Wow! I've changed [image drawInRect:CGRectMake(0, rect.size.height - 75, rect.size.width, 75) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; to [image drawInRect:CGRectMake(rect.origin.x, [self bounds].size.height - 75, rect.size.width, 75) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; and that solved the problem. Could you explain why? – Denis Kildishev Jan 13 '14 at 00:40