自定义图形的无边框NSWindow出现意外边框

3

我有一个无边框的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];
}

然而,每隔一段时间(大概十次中有一次),当我启动应用程序时,图形看起来不对,因为窗口周围有一个灰色的一个像素的正方形边框。它没有设置在我的自定义图形周围,而是在窗口的框架周围,这意味着它抵消了我的圆角。
我的子类中是否有我遗漏的东西?
编辑: 以下是问题的屏幕截图:

好像我之前也遇到过这个问题,但是我已经解决了。可以看这里:http://stackoverflow.com/questions/9124349/grey-border-around-view-when-using-nsborderlesswindowmask - sudo rm -rf
抱歉,Sam,我没有这样做。最终我使用了一个不太定制化的设计,其中窗口边缘保持标准。 - pajevic
1个回答

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]]; 这是我之前说过的。

还有边框吗?

对我来说它有效。


嗨,贾斯汀。谢谢你的回复。不幸的是,你的建议没有解决这个问题。 - pajevic
嗯...那应该可以。也许你可以添加屏幕截图以更清楚地了解出了什么问题。 - Justin Boo
我现在已经添加了一个问题的截图。 - pajevic
1
谢谢您的努力。然而问题仍然存在。只是为了澄清,我没有在创建圆角上遇到问题;在我的情况下,它们来自背景图像。我的问题是有时候会出现“不请自来”的方形边框。因此,我真正想做的是摆脱那个不必要的边框,这也会使用您的解决方案产生。 - pajevic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接