问题
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