Does NSView drawRect interfere with subviews?

I have nsview and I use draw rect to draw an image for the background. It also has 3 nsbuttons sub. The problem is that whenever the mouse falls over the button, the rest of the buttons disappear. But when I remove the draw draw method, this does not happen. Therefore, I assume this is due to the draw rect method for drawing images.

How can i avoid this? Thank.

EDIT: Ok, I figured where the problem is. Basically, I have NSMenuItem, and I put 3 buttons in it. But at NSMenu, at the top, there is a 4 pixel spacer. Therefore, basically, to remove this add-on, I used the solution provided here: Gap above the user view NSMenuItem

From the solution there is a line in the drawRect method:

[[NSBezierPath bezierPathWithRect:fullBounds] setClip];

The moment I delete this line, and the button behaves correctly. But then the gasket on top does not go away.

Here is my drawRect:

- (void) drawRect:(NSRect)dirtyRect {

    [[NSGraphicsContext currentContext] saveGraphicsState];

    NSRect fullBounds = [self bounds];
    fullBounds.size.height += 4;
    [[NSBezierPath bezierPathWithRect:fullBounds] setClip];

    NSImage *background = [NSImage imageNamed:@"bg.png"];
    [background drawInRect:fullBounds fromRect:NSZeroRect operation:NSCompositeCopy fraction:100.0];

    [[NSGraphicsContext currentContext] restoreGraphicsState];
}
+5
source share
2 answers

Solving a related issue does not include saving and restoring the state of the graphics, which is a good idea when changing the one you did not create. Try:

- (void)drawRect:(NSRect)dirtyRect {
   // Save the current clip rect that has been set up for you
   [NSGraphicsContext saveGraphicsState];
   // Calculate your fullBounds rect
   // ...
   // Set the clip rect
   // ...
   // Do your drawing
   // ...
   // Restore the correct clip rect
   [NSGraphicsContext restoreGraphicsState]
+3
source

Are you sure that these buttons are actually subzones, and not just located above the view that you are drawing?

0
source

All Articles