我正试图找出我的菜单栏应用程序出现的一个奇怪问题。它可以运行几个小时甚至几天,但会随机从菜单栏中消失。
在活动监视器中,它仍在后台运行。应用程序中有一个全局键盘快捷键来显示窗口,它可以显示应用程序,没有问题,但菜单栏图标仍然缺失。
我在macOS Monterery 12.2.1上
状态条形控制器
class StatusBarController {
private var statusBar: NSStatusBar
private var statusItem: NSStatusItem
public var popover: NSPopover
private var eventMonitor: EventMonitor?
init(_ popover: NSPopover)
{
self.popover = popover
statusBar = NSStatusBar.init()
statusItem = statusBar.statusItem(withLength: 28.0)
if let statusBarButton = statusItem.button {
statusBarButton.image = #imageLiteral(resourceName: "link")
statusBarButton.image?.size = NSSize(width: 18.0, height: 18.0)
statusBarButton.image?.isTemplate = true
statusBarButton.action = #selector(togglePopover(sender:))
statusBarButton.target = self
}
}
.......}
程序委托
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var popover = NSPopover.init()
var statusBar: StatusBarController?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the contents
let contentView = ContentView()
// Set the SwiftUI's ContentView to the Popover's ContentViewController
popover.contentViewController = MainViewController()
popover.contentViewController?.view = NSHostingView(rootView: contentView)
popover.animates = false
KeyboardShortcuts.onKeyUp(for: .triggerPopover, action: {
self.statusBar?.togglePopover(sender: self)
})
// Create the Status Bar Item with the Popover
statusBar = StatusBarController.init(popover)
}
}