如何使用 swift 为 OS X 应用程序的标题栏着色
How to color the title bar of the OS X app with swift
我正在使用 Xcode 7.2.1 和 Swift 2 来开发 OS X 应用程序。我希望应用程序充满颜色。如何按照上面的示例图片进行操作?
我这样做是为了去掉标题栏:
override func viewDidAppear() {
super.viewDidAppear()
self.view.wantsLayer = true
self.view.window?.titlebarAppearsTransparent = true
self.view.window?.movableByWindowBackground = true
self.view.window?.titleVisibility = NSWindowTitleVisibility.Hidden
}
使用您的背景颜色添加自定义子视图。或者(更好)将该自定义视图设置为您 window 的内容视图。
如果我将您的 "get rid of the title bar" 代码移动到 AppDelegate
并像这样设置 window.backgroundColor
:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
window?.titlebarAppearsTransparent = true
window.movableByWindowBackground = true
window.titleVisibility = NSWindowTitleVisibility.Hidden
window.backgroundColor = NSColor.redColor()
let viewController = ViewController()
window.contentViewController = viewController
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
然后只需在视图控制器中设置颜色即可:
override func viewDidLoad() {
super.viewDidLoad()
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.redColor().CGColor
}
我最终得到这个 window
有趣的是我必须在两个地方都设置颜色,所以@joshua-nozzi 建议的解决方案可能是一个更安全的选择
备选
如果只想给整个NSWindow设置一个背景色,隐藏标题栏,可以将其styleMask设置为textured:
self.view.window?.styleMask = NSTexturedBackgroundWindowMask
self.view.window?.backgroundColor = NSColor.redColor()
这样做的好处:它至少向后兼容 OS X 10.6
创建 window 后,只需将其添加到您的 AppDelegate 中:
window.titlebarAppearsTransparent = true
window.backgroundColor = .red
使用 OSX 10.15 进行测试,并创建了一个作为 SwiftUI 应用程序的项目。
我正在使用 Xcode 7.2.1 和 Swift 2 来开发 OS X 应用程序。我希望应用程序充满颜色。如何按照上面的示例图片进行操作?
我这样做是为了去掉标题栏:
override func viewDidAppear() {
super.viewDidAppear()
self.view.wantsLayer = true
self.view.window?.titlebarAppearsTransparent = true
self.view.window?.movableByWindowBackground = true
self.view.window?.titleVisibility = NSWindowTitleVisibility.Hidden
}
使用您的背景颜色添加自定义子视图。或者(更好)将该自定义视图设置为您 window 的内容视图。
如果我将您的 "get rid of the title bar" 代码移动到 AppDelegate
并像这样设置 window.backgroundColor
:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
window?.titlebarAppearsTransparent = true
window.movableByWindowBackground = true
window.titleVisibility = NSWindowTitleVisibility.Hidden
window.backgroundColor = NSColor.redColor()
let viewController = ViewController()
window.contentViewController = viewController
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
然后只需在视图控制器中设置颜色即可:
override func viewDidLoad() {
super.viewDidLoad()
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.redColor().CGColor
}
我最终得到这个 window
有趣的是我必须在两个地方都设置颜色,所以@joshua-nozzi 建议的解决方案可能是一个更安全的选择
备选
如果只想给整个NSWindow设置一个背景色,隐藏标题栏,可以将其styleMask设置为textured:
self.view.window?.styleMask = NSTexturedBackgroundWindowMask
self.view.window?.backgroundColor = NSColor.redColor()
这样做的好处:它至少向后兼容 OS X 10.6
创建 window 后,只需将其添加到您的 AppDelegate 中:
window.titlebarAppearsTransparent = true
window.backgroundColor = .red
使用 OSX 10.15 进行测试,并创建了一个作为 SwiftUI 应用程序的项目。