Custom NSWindow in 10.9 Doesn't show shadow when SetOpaque:NO

浪子不回头ぞ 提交于 2019-12-13 20:00:34

问题


So I have created an NSWindow (with rounded corners), and in 10.10, it has a shadow around it. However when I tested in 10.9, the shadow disappeared. I have set breakpoints at every possible point, and [window hasShadow] is always YES.

If I set [self setOpaque:YES] in the initWithContentRect method of the window, the shadow comes back.

Has anybody seen this before? Or know what could possibly cause this?

It appears the hasShadow property doesn't do anything because if I set it to YES/NO it doesn't change anything. Just setting it opaque/transparent makes the shadow appear/disappear

Thanks in advance!


回答1:


Here is how I finally managed this.

First of all, this will happen only if you are using layered back views (and this is our case if we want to easily implement rounded corners), in the RoundTransparentWindow Apple sample you can test it, until you not make the CutomView layered you will see the window shadow on 10.9 too, adding [self setWantsLayer:YES]; will kill your shadow.

The key for the solution here is adding all the layered views to a view that has no layer at all and making that view to the window contentView. That new contentView should reimplement only the drawRect: method on the following way:

- (void)drawRect:(NSRect)dirtyRect
{
    [NSGraphicsContext saveGraphicsState];

    [[NSColor clearColor] set];
    NSRectFill(dirtyRect);

    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:[self bounds]
                                                     xRadius:cCornerRadius
                                                     yRadius:cCornerRadius];
    [[NSColor whiteColor] set];
    [path fill];

    [NSGraphicsContext restoreGraphicsState];
}

"Pre drawing" the rounded corners filled with any NONE transparent color is the key here, it will make the window server use the shadow again even if [self setOpaque:NO]; self.backgroundColor = [NSColor clearColor]; set during the window construction, that, as you found it earlier, made the server to think the window is transparent and does not need shadow.



来源:https://stackoverflow.com/questions/27551093/custom-nswindow-in-10-9-doesnt-show-shadow-when-setopaqueno

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!