如何在OSX Swift中使窗口透明? [英] How to make window transparent in osx swift?
问题描述
我在Xcode中创建了一个空项目.
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.
我进行了很多搜索,但是每个人都使用window
变量进行更改,例如在这里,但是我该如何创建NSWindow
实例?
I've searched a lot but everyone uses a window
variable to change like here but how can I create NSWindow
instance?
我是Mac应用程序开发的新手.有人可以写出详细的答案吗?
I'm new to mac app development. So can anyone write a detailed answer?
谢谢!
推荐答案
您可以按如下方式获取应用程序窗口的实例:
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
从NSApp获取窗口(共享应用程序实例的全局常量):
Get your window from NSApp (global constant for the shared app instance):
guard let window = NSApp.windows.first else { return }
或覆盖viewWillAppear
或viewDidAppear
并访问视图的window属性.
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
}
这篇关于如何在OSX Swift中使窗口透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!