代码之家  ›  专栏  ›  技术社区  ›  swiftnoob

NSStatus项目图标随机消失

  •  0
  • swiftnoob  · 技术社区  · 2 年前

    我正试图找出我的菜单栏应用程序出现的一个奇怪问题。它可以运行几个小时甚至几天,但会随机从菜单栏中消失。

    在活动监视器中,它仍在后台运行。应用程序中有一个全局键盘快捷键来显示窗口,它可以显示应用程序,没有问题,但菜单栏图标仍然缺失。

    我在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)
        }
    
    
        
    }
    
    0 回复  |  直到 2 年前
        1
  •  1
  •   Willeke    2 年前

    从…起 Status Bar Programming Topics :

    系统范围的状态栏位于菜单栏的右侧,是当前唯一可用的状态栏。

    NSStatusBar.init() 不会返回系统状态栏。使用 NSStatusBar.system 相反

    推荐文章