Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

macos - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...