unknownymouse
unknownymouse

Reputation: 409

How to make window transparent in osx swift?

I have a empty created project in Xcode.

When I press run button, it displays a window. How can I change it's appearance like transparency of window etc.

I've searched a lot but everyone uses a window variable to change like here but how can I create NSWindow instance?

I'm new to mac app development. So can anyone write a detailed answer?

Thank you!

Upvotes: 1

Views: 3417

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236340

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
}

Upvotes: 11

Related Questions