本文介绍了把一个透明的NSWindow永久地放在另一个NSWindow的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 NSWebView 之上有一些UI控件,因为这个问题我现在要添加一个透明 NSWindow ,所以没有关闭按钮等,在 NSWebView 的顶部,因此,在当前 NSWindow

I want to have some UI controls on top of a NSWebView and because of this problem " Video in NSWebView hides views on top of the NSWebView " I now want to add a "transparent" NSWindow, so without the close buttons etc., on top of my NSWebView, hence, on top of my current NSWindow.

如何实现这一点,并确保这个覆盖窗口保持原位,即使我移动底层窗口?

How can I achieve this and make sure that this "overlay window" stays in place, even if I move the underlying window?

编辑:@ dzolanta的方法工作正常,我想知道是否可以通过使用 NSWindowController 这将允许我正确使用Outlets等。

: While @dzolanta's approach works fine, I wonder if it is possible to do it by using an NSWindowController which would allow me to properly use Outlets etc.

推荐答案

子窗口是你需要的

使用 NSBorderlessWindowMask 创建 NSWindow 透明使用 - setOpaque: - setBackgroundColor:方法。然后将新创建的窗口添加为包含 NSWebView (使用 NSWindow - addChildWindow:ordered:方法)。移动父窗口会自动导致子窗口移动。

Create NSWindow with NSBorderlessWindowMask and define it to be transparent using - setOpaque: and - setBackgroundColor: methods. Then add newly created window as a child of window containing an instance of NSWebView (using NSWindow's - addChildWindow:ordered: method). Moving parent window will automatically cause child window to move.

使用工作代码更新

CGRect wRect = self.window.frame;
NSView *contentView  =self.window.contentView;
CGRect cRect = contentView.frame;

CGRect rect = CGRectMake(wRect.origin.x, wRect.origin.y, cRect.size.width, cRect.size.height);
NSWindow *overlayWindow = [[NSWindow alloc]initWithContentRect:rect 
                                                     styleMask:NSBorderlessWindowMask 
                                                       backing:NSBackingStoreBuffered 
                                                         defer:NO];
overlayWindow.backgroundColor = [NSColor redColor];
[overlayWindow setOpaque:NO];
overlayWindow.alphaValue = 0.5f;

[self.window addChildWindow:overlayWindow ordered:NSWindowAbove];

这篇关于把一个透明的NSWindow永久地放在另一个NSWindow的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 00:27