我有一个 nsview,我使用 draw rect 为背景绘制图像。它还有 3 个子视图 nsbuttons。问题是,只要鼠标按下按钮,其他按钮就会消失。但是当我删除 draw rect 方法时,这不会发生。所以我猜这与绘制图像的draw rect方法有关。
我怎样才能避免这种情况?谢谢。
编辑:好的,我知道问题出在哪里。基本上,我有一个 NSMenuItem,我在里面放了一个带有 3 个按钮的视图。但是在 NSMenu 的顶部,有一个 4 像素的填充。因此,基本上,为了删除该填充,我使用了此处提供的解决方案: Gap above NSMenuItem custom view
在解决方案中,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];
}