Неожиданная граница вокруг NSWindow без рамки с пользовательской графикой - PullRequest
3 голосов
/ 29 февраля 2012

У меня есть подкласс без полей 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];
}

Однако, время от времени (возможно, 1 из 10), когда я запускаю приложение, графика выглядит неправильно, потому что вокруг окна появляется серая квадратная граница в один пиксель.Он не установлен вокруг моей пользовательской графики, а вокруг рамки окна, что означает, что он сводит на нет мои закругленные углы.

Есть ли что-то, чего мне не хватает в моих подклассах?

РЕДАКТИРОВАТЬ: Вот скриншот проблемы:

enter image description here

1 Ответ

0 голосов
/ 29 февраля 2012

Вам нужно установить цвет фона на прозрачный.Добавьте это к своему 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]];что мне сказали, говорите.

Все еще с границей?

Это работает для меня.

...