Place a transparent NSWindow permanently on top of another NSWindow

I want to have user interface controls on top of NSWebView and because of this problem https://stackoverflow.com/questions/9120868/video-in-nswebview-hides-views-on-top-of-the-nswebview "Now I want add a "transparent" NSWindow , so without the close buttons, etc. on top of my NSWebView , therefore, on top of the current NSWindow .

How can I achieve this and make sure that this overlay window remains in place, even if I move the main window?

EDIT:. Although the @ dzolanta approach works fine, I wonder if this can be done using NSWindowController , which will allow me to use Outlets properly, etc.

+7
source share
2 answers

A baby window is what you need.

Create an NSWindow using NSBorderlessWindowMask and determine its transparency using the methods - setOpaque: and - setBackgroundColor: Then add the newly created window as a child of the window containing the NSWebView instance (using the NSWindow method - addChildWindow:ordered: . Moving the parent window will automatically move the child window.

Update using working code :

 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]; 
+20
source

Swift 3 version using a window controller:

 final class OverlayWindowController: NSWindowController { init(frame: NSRect) { let window = NSWindow(contentRect: frame, styleMask: .borderless, backing: .buffered, defer: false) super.init(window: window) window.contentViewController = MyViewController() window.backgroundColor = NSColor.clear window.isOpaque = false } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") } } 
0
source

All Articles