<分区>
我正在尝试更改我的 OSX 应用程序的窗口颜色。
我找不到关于如何做的信息,但我看到了一些例子。
我试过:
在我的 AppDelegate 中:
NSApplication.shared.mainWindow?.backgroundColor = NSColor.black
哪个不起作用。
在我的 View Controller 中:
self.view.window?.backgroundColor = NSColor.blue
<分区>
我正在尝试更改我的 OSX 应用程序的窗口颜色。
我找不到关于如何做的信息,但我看到了一些例子。
我试过:
在我的 AppDelegate 中:
NSApplication.shared.mainWindow?.backgroundColor = NSColor.black
哪个不起作用。
在我的 View Controller 中:
self.view.window?.backgroundColor = NSColor.blue
最佳答案
好的,所以我假设您在这里使用 Storyboard(否则 Xcode 创建的模板会在 AppDelegate
中为您提供一个 window
属性,您可能会会成功地尝试设置背景颜色)。
有很多选择如何做到这一点,但基本上你会使用一个 NSWindow
子类或一个 NSWindowController
子类来负责设置窗口外观(对于那些属性至少不能在 Interface Builder 中设置)
NSWindow 子类:
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
}
}
并将这个子类设置为 Storyboard中的类。
NSWindowController 子类:
class BlackWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
window?.backgroundColor = .black
window?.titlebarAppearsTransparent = true
}
}
然后再次在 Storyboard中设置类。您可以在窗口 Controller 中修改窗口的位置有多种变体,windowDidLoad()
只是一种建议。
希望这对您有所帮助。
关于Swift - Cocoa - 更改窗口颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48938287/