【问题标题】:Unexpected border around a borderless NSWindow with custom graphics带有自定义图形的无边框 NSWindow 周围出现意外边框
【发布时间】:2012-02-29 07:34:52
【问题描述】:

我有一个NSWindow 的无边框子类,其中包含带圆角的自定义图形:

我的自定义窗口

- (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;
}

我的自定义视图

- (void)drawRect:(NSRect)rect {
    [[NSColor clearColor] set];
    NSRectFill([self frame]);
    [backgroundImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
}

但是,每隔一段时间(可能 10 次中的 1 次),当我启动应用程序时,图形看起来不对,因为我在窗口周围看到一个灰色的 1 像素方形边框。它不是围绕我的自定义图形而是围绕窗口的框架设置的,这意味着它否定了我的圆角。

我的子类中是否缺少某些东西?

编辑: 这是问题的截图:

【问题讨论】:

  • 似乎我遇到了同样的问题,我已经解决了。见这里:stackoverflow.com/questions/9124349/…
  • 你有没有想过这个问题?我也有同样的问题。
  • 对不起山姆,我没有。我最终使用了一种定制化程度较低的设计,其中窗户边缘保持为标准。

标签: objective-c cocoa nswindow


【解决方案1】:

您需要将背景颜色设置为透明颜色。将此添加到您的 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]];我之前说过的话。

还有边框吗?

它对我有用。

【讨论】:

  • 嗨贾斯汀。感谢您的答复。很遗憾,您的建议并未解决问题。
  • 嗯..应该可以。也许您可以添加屏幕截图以便更清楚地了解问题所在。
  • 我现在添加了问题的截图。
  • 感谢您的努力。但是,问题仍然存在。澄清一下,创建圆边没有问题;就我而言,它们来自背景图像。我的问题是有时方形边框“不请自来”。所以我真正想做的是摆脱不需要的边框,这也是使用您的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多