【发布时间】:2020-02-03 09:30:30
【问题描述】:
我目前有一个 NSStatusBar 和一个 NSStatusBarItem 并显示标题。但我不想用自定义NSView 替换标题/文本。我该怎么做?
我目前有:
func applicationDidFinishLaunching(_ aNotification: Notification) {
let statusBar = NSStatusBar.system
statusBarItem = statusBar.statusItem(withLength: NSStatusItem.variableLength)
statusBarItem.button?.title = "My Menu Bar App"
}
假设我不想用以下内容替换标题:
let view = NSView(frame: NSRect(x: 0, y: 0, width: 40, height: 40))
view.layer?.backgroundColor = NSColor.yellow.cgColor
我怎样才能做到这一点?我尝试将其作为子视图添加到按钮中,但没有成功。
【问题讨论】:
-
将视图添加到按钮的子视图中
-
@vadian 正如我所写:“我试图将它作为子视图添加到按钮中,但没有成功。”
-
原因是别的:与
UIView不同,您必须使用view.wantsLayer = true启用NSView中的层。还要考虑自定义视图的高度限制为状态栏的高度。 -
我已将
.wantsLayer = true添加到视图和按钮中,但仍然没有显示。只是一个空白的可点击方块,但看不到任何颜色。 -
我还在使用
statusBarItem = statusBar.statusItem(withLength: NSStatusItem.squareLength)来获得固定大小。
标签: swift macos cocoa nsstatusitem nsstatusbar