Mac App开发随笔

随手记录......

隐藏Dock图标
  • info.plist配置 Application is agent (UIElement)
NSWindow
// 获取主window
let window = NSApplication.shared.windows.first
// 设置window可以背景拖动
window?.isMovableByWindowBackground = true
// 设置window全屏显示置顶
window?.level = NSWindow.Level.init(rawValue: 21)
NSMenu
  • 实例statusItem
private var statusItem: NSStatusItem = {
   let statusBar = NSStatusBar.system
   let item = statusBar.statusItem(withLength: NSStatusItem.squareLength)
   item.button?.image = NSImage(named: "home_service_Btn")
   return item
}()
  • 实现NSMenude 示例
    func addStatusItem() {
        let mainMenu = NSMenu(title: "111")
        for (index, value) in menuArr.enumerated() {
            let menuItem = NSMenuItem(title: value, action: #selector(selectMenu(_:)), keyEquivalent: "")
            menuItem.tag = 1000 + index
            menuItem.target = self
            if index == 2 || index == 3 || index == 5 {
                // 按需进行item分隔
                mainMenu.addItem(.separator())
            }
            mainMenu.addItem(menuItem)
        }
        
        let timeMenu = NSMenu(title: "222")
        for (index, value) in timeArr.enumerated() {
            let menuItem = NSMenuItem(title: value, action: #selector(makeTimerRun(_:)), keyEquivalent: "")
            menuItem.target = self
            menuItem.tag = 2000 + index
            timeMenu.addItem(menuItem)
        }
        ...... 此处省略重复代码
        // menu添加子menu
        mainMenu.setSubmenu(timeMenu, for: mainMenu.item(at: 6)!)
        statusItem.menu = mainMenu
    }
退出应用
NSApp.terminate(self)

你可能感兴趣的:(Mac App开发随笔)