如何在OSX上使用swift创建具有透明背景的窗口? [英] How can I create a window with transparent background with swift on osx?
问题描述
我想在我的Mac上创建一个osx/cocoa应用程序,该应用程序非常简单:在我的mac上显示文本字符串,没有背景.最终,这将是一个计时器,该计时器在其他窗口上方显示为覆盖图,而不会太麻烦.
I want to create an osx/cocoa application on my mac, which does something very simple: Display a text string on my mac, with no background. Ultimately this will be a timer which displays as an overlay on top of other windows, without being too intrusive.
我尝试在applicationDidFinishLaunching
中设置window.backgroundColor = NSColor(red: 1.0, green:0.5, blue:0.5, alpha: 0.5)
(请参见alpha为0.5),但这并不能将其转变为远程透明的东西.
I tried setting window.backgroundColor = NSColor(red: 1.0, green:0.5, blue:0.5, alpha: 0.5)
(see the alpha is 0.5), in applicationDidFinishLaunching
but this doesn't turn it into something remotely transparent.
任何善良的灵魂都想提出一种方法来做到这一点?
Any good soul wants to suggest a way to do this?
推荐答案
NSWindow具有属性"opaque",默认情况下为true.
NSWindow has a property 'opaque' which it is true by default.
当窗口不透明时,此属性的值为true;否则为false. 否则为假.
The value of this property is true when the window is opaque; otherwise, false.
只需将其更改为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)
}
迅速4更新: opaque
已重命名为
查看全文