9中的自定义NSWindow不显示阴影

9中的自定义NSWindow不显示阴影

本文介绍了SetOpaque:NO时10.9中的自定义NSWindow不显示阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了一个NSWindow(带有圆角),在10.10中,它周围有阴影.但是,当我在10.9中进行测试时,阴影消失了.我在每个可能的点都设置了断点,并且 [window hasShadow] 始终为.

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.

如果我在窗口的 initWithContentRect 方法中设置了 [self setOpaque: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?

看来, hasShadow 属性没有任何作用,因为如果我将其设置为YES/NO,它什么也不会改变.只需将其设置为不透明/透明即可使阴影出现/消失

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

提前谢谢!

推荐答案

这是我最终如何解决此问题的方法.

Here is how I finally managed this.

首先,只有在使用分层后视图(如果我们要轻松实现圆角的情况就是这种情况)时,才会在 RoundTransparentWindow 苹果示例,您可以对其进行测试,直到您未将CutomView分层时,您也会在10.9上看到阴影,添加 [self setWantsLayer:YES]; 将杀死您的影子.

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.

此处解决方案的关键是将所有分层视图添加到根本没有层的视图中,并将该视图添加到窗口contentView中.新的contentView应该仅通过以下方式重新实现 drawRect:方法:

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

此处的关键是预绘制"填充有任何NONE透明颜色的圆角,即使 [self setOpaque:NO];也将使窗口服务器再次使用阴影.self.backgroundColor = [NSColor clearColor]; 在窗口构建过程中进行了设置,正如您之前发现的那样,它使服务器认为该窗口是透明的并且不需要阴影.

"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.

这篇关于SetOpaque:NO时10.9中的自定义NSWindow不显示阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:03