我有一个NSWindow
的无边框子类,带有圆角的自定义图形:
MyCustomWindow :
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self) {
// Start with no transparency for all drawing into the window
[self setAlphaValue:1.0];
// Turn off opacity so that the parts of the window that are not drawn into are transparent.
[self setOpaque:NO];
[self setMovableByWindowBackground:YES];
}
return self;
}
- (BOOL) canBecomeKeyWindow
{
return YES;
}
MyCustomView :
- (void)drawRect:(NSRect)rect {
[[NSColor clearColor] set];
NSRectFill([self frame]);
[backgroundImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
}
但是,当我启动应用程序时,每隔一段时间(可能是10分之一)中的图形看起来就错了,因为我在窗口周围有一个灰色的单像素方形边框。它不是围绕我的自定义图形设置的,而是围绕窗口的框架,这意味着它会抵消我的圆角。
我的子类中是否缺少某些内容?
修改 以下是该问题的屏幕截图:
答案 0 :(得分:0)
您需要将背景颜色设置为透明色。将其添加到您的 MyCustomWindow :
[self setBackgroundColor:[NSColor clearColor]];
您的 MyCustomWindow 应如下所示:
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self) {
// Start with no transparency for all drawing into the window
[self setAlphaValue:1.0];
[self setBackgroundColor:[NSColor clearColor]];
// Turn off opacity so that the parts of the window that are not drawn into are transparent.
[self setOpaque:NO];
[self setMovableByWindowBackground:YES];
}
return self;
}
- (BOOL) canBecomeKeyWindow
{
return YES;
}
<强>更新强>
尝试编辑您的drawRect:
将其替换为:
- (void)drawRect:(NSRect)rect {
NSBezierPath * path;
path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:6 yRadius:6];
[[NSColor redColor] set];
[path fill];
/* If this example will help You. Replace redColor to clearColor and use this instead RectFill */
}
*还要添加 MyCustomWindow [self setBackgroundColor:[NSColor clearColor]];我说的是耳语。
还有边境吗?
这对我有用。