How to create a window with a transparent background with osx quick setup?

I want to create an osx / cocoa application on my mac, which makes something very simple: display a text string on my mac, without background. Ultimately, it will be a timer that appears as an overlay on top of other windows, without being too intrusive.

I tried setting window.backgroundColor = NSColor(red: 1.0, green:0.5, blue:0.5, alpha: 0.5) (see alpha 0.5), to applicationDidFinishLaunching , but that would not turn it into something remotely transparent.

Any good soul want to suggest a way to do this?

+13
cocoa swift macos
source share
4 answers

NSWindow has an opaque property, which defaults to true.

The value of this property is true when the window is opaque; otherwise false.

Just change it to false:

 override func viewWillAppear() { super.viewWillAppear() view.window?.opaque = false view.window?.backgroundColor = NSColor(red: 1, green: 0.5, blue: 0.5, alpha: 0.5) } 

Swift 4 update: opaque been renamed to isOpaque

 override func viewWillAppear() { super.viewWillAppear() view.window?.isOpaque = false view.window?.backgroundColor = NSColor(red: 1, green: 0.5, blue: 0.5, alpha: 0.5) } 
+7
source share

Make the window opaque and give it a clear background:

 func applicationDidFinishLaunching(aNotification: NSNotification) { window.opaque = false window.backgroundColor = NSColor.clearColor() } 
+12
source share

Small update for Swift 3

An example of a subclass of a comment box:

 class customWindow: NSWindow { override init(contentRect: NSRect, styleMask style: NSWindowStyleMask, backing bufferingType: NSBackingStoreType, defer flag: Bool) { super.init(contentRect: contentRect, styleMask: style, backing: bufferingType, defer: flag) // Set the opaque value off,remove shadows and fill the window with clear (transparent) self.isOpaque = false self.hasShadow = false self.backgroundColor = NSColor.clear // Change the title bar appereance self.title = "My Custom Title" //self.titleVisibility = .hidden self.titlebarAppearsTransparent = true } 
+3
source share

Swift 3/4

 self.window?.isOpaque = false self.window?.hasShadow = false self.window?.backgroundColor = NSColor.clear self.window?.titlebarAppearsTransparent = true 
+1
source share

All Articles