NSView drawRect干扰子视图?

| 我有一个nsview,我使用draw rect绘制背景图像。它还具有3个子视图nsbutton。问题是,每当鼠标按下按钮时,其他按钮就会消失。但是,当我删除draw rect方法时,这不会发生。因此,我猜测这与绘制图像的draw rect方法有关。 如何避免这种情况? 谢谢。 编辑: 好的,我知道问题出在哪里。基本上,我有一个NSMenuItem,并在其中带有3个按钮的视图。但是在NSMenu顶部,填充为4个像素。因此,基本上,要删除该填充,我使用了此处提供的解决方案: NSMenuItem自定义视图上方的间隙 在解决方案中,drawRect方法中有一行:
[[NSBezierPath bezierPathWithRect:fullBounds] setClip];
此刻,我删除了这一行,并且按钮正常运行。但是然后,顶部的填充不会消失。 这是我的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];
}
    
已邀请:
链接问题的解决方案不包括保存和恢复图形状态,当您修改未创建的图形时,这是一个好主意。试试看:
- (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]
    
您确定这些按钮实际上是子视图,而不仅仅是放置在您正在绘制的视图上吗?     

要回复问题请先登录注册