How can I create a window with transparent background with swift on osx?
NSWindow has a property 'opaque' which it is true by default.
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
has been renamed 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)
}
How to make window transparent in osx swift?
You can get an instance of your application window as follow:
guard let window = NSApplication.shared().windows.first else { return }
window.isOpaque = false
window.backgroundColor = .clear
Get your window from NSApp (global constant for the shared app instance):
guard let window = NSApp.windows.first else { return }
Or override viewWillAppear
or viewDidAppear
and access your view's window property.
override func viewWillAppear() {
super.viewWillAppear()
view.window?.isOpaque = false
view.window?.backgroundColor = .clear
}
Making the title bar of a window completely transparent
The problem is that inside viewDidLoad method the window property is always nil. All the optional chaining in your code just fails silently. You just need to move your code to viewWillAppear method.
override func viewWillAppear() {
super.viewWillAppear()
// configure your window properties here
}
Swift - Cocoa - Change window color?
Ok, so I am assuming you are using Storyboards here (because otherwise the template Xcode creates would have given you a window
property in the AppDelegate
and you'd probably would have successfully tried setting the background color on that).
There are many options how to do this but basically you would use an NSWindow
subclass or an NSWindowController
subclass to be responsible for setting the windows appearance (for those properties that can not be set in Interface Builder at least)
NSWindow subclass:
class BlackWindow: NSWindow {
override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
backgroundColor = .black
}
}
and set this subclass as the class in the storyboard.
NSWindowController subclass:
class BlackWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
window?.backgroundColor = .black
window?.titlebarAppearsTransparent = true
}
}
and again set the class in the storyboard. There are many variants of where you can modify your window in the window controller, windowDidLoad()
is just one suggestion.
Hope this helps.
Passing Click Through Transparent Window
Setting IgnoresMouseEvents to YES should do the trick..
- (void)setIgnoresMouseEvents:(BOOL)ignoreMouseEvents
Specifies whether the window is transparent to mouse clicks and other mouse events, allowing overlay windows.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/setIgnoresMouseEvents:
Related Topics
Expected to Decode Array<Any> But Found a Dictionary Instead
Why Is Manually Setup Root View Controller Showing Black Screen
Why Is There No Universal Base Class in Swift
Access Input from Uialertcontroller
How to Create a View-Based Nstableview Purely in Code
Swift Semantics Regarding Dictionary Access
Dynamic Row Hight Containing Texteditor Inside a List in Swiftui
Uicollectionview - Resizing Cells on Device Rotate - Swift
Dispatchsourcetimer and Swift 3.0
Upvote/Downvote System Within Swift via Firebase
Swift Extension: Same Extension Function in Two Modules
Swift Nspredicate Throwing Exc_Bad_Access(Code=1, Address=0X1) When Compounding Statements
Swift - Unit Testing Private Variables and Methods
A Swift Protocol Requirement That Can Only Be Satisfied by Using a Final Class
How to Call a Function When a Swiftui Picker Selection Changes