如何使用Swift在运行时使我的应用程序表现得就像我在更改Application is agent(UIElement)?

托尼·斯塔克4ever

我正在编码NSPopover菜单栏右侧的Mac应用程序Application is agent(UIElement)设置为YES)。我允许用户通过单击并向下拖动弹出窗口来分离弹出窗口,这会将应用程序置于窗口内。这很好。但是,当将应用程序从菜单栏拖出并制成一个窗口时,我希望我的应用程序图标出现在停靠栏中,并希望在菜单栏的左侧显示特定于应用程序的菜单,就像Application is agent(UIElement)设置为NO相反,当关闭窗口并将应用程序返回到菜单栏中的弹出窗口时,我希望我的应用程序图标从停靠栏中消失,并且不再在菜单栏的左侧显示特定于应用程序的菜单(Application is agent(UIElement)已设置返回YES)。

这个问题中,我了解到Application is agent(UIElement)无法在运行时进行更改但是,给出的答案是在Objective-C中进行的,自OS X 10.9起,最后一个功能似乎已被淘汰。如何使我的应用具有与Application is agent(UIElement)使用Swift在运行时更改相同的行为

我知道会在中显示应用程序图标/菜单栏菜单,而在中windowDidBecomeMain隐藏应用程序图标/菜单栏菜单windowWillClose

谢谢。

托尼·斯塔克4ever

经过大量的反复试验,但我终于弄清楚了。Application is agent(UIElement)您可以使用而不是使用NSApp.setActivationPolicy现在这是我的代码。在应用程序委托中:

var isWindow = false

class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {

    let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
    let popover = NSPopover()

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application

        NSApp.setActivationPolicy(.accessory)

        if let button = statusItem.button {
            button.image = NSImage(named: "StatusBarImage")
            button.action = #selector(togglePopover(_:))
        }
        popover.contentViewController = MainViewController.loadController()
        popover.delegate = self
        popover.animates = false
        popover.behavior = .transient
    }

    @objc func togglePopover(_ sender: Any?) {
        if popover.isShown == true {
            popover.performClose(sender)
        } else if detachedWindowController.window!.isVisible {
            detachedWindowController.window?.setIsVisible(false)
            isWindow = true
        } else if isWindow == true {
            detachedWindowController.window?.setIsVisible(true)
        } else {
            if let button = statusItem.button {
                popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
            }
        }
    }

    lazy var detachedWindowController: DetachedWindowController = {
        let detachedWindowController = DetachedWindowController(windowNibName: "DetachedWindowController")
        detachedWindowController.contentViewController = MainViewController.loadController()
        return detachedWindowController
    }()

    func popoverShouldDetach(_ popover: NSPopover) -> Bool {
        return true
    }

    func detachableWindow(for popover: NSPopover) -> NSWindow? {
        return detachedWindowController.window
    }
}

DetachedWindowController

class DetachedWindowController: NSWindowController, NSWindowDelegate {

    @IBOutlet var detachedWindow: NSWindow!

    override func windowDidLoad() {
        super.windowDidLoad()

        detachedWindow.delegate = self
    }

    func windowWillClose(_ notification: Notification) {
        isWindow = false
        NSApp.setActivationPolicy(.accessory)
    }

    func windowDidBecomeMain(_ notification: Notification) {
        if NSApp.activationPolicy() == .accessory {
            NSApp.setActivationPolicy(.regular)
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在iOS Swift中进行本地化。我如何在运行时更改应用程序语言

如何更改UIElement的值

更改UIElement的角度

使用MVVM处理UIElement

Tomcat 7-在运行时获取应用程序名称,而无需通过java-agent / aspectj登录

如何编写可以在运行时进行自我更新的Java应用程序?

如何在运行时中检测到应用程序使用了Swift

如何使Playground执行时间就像我们在iOS应用程序中运行一样快

运行时如何自我更新应用程序

属性更改时如何更改 UIElement?

如何在运行时检查我的Rails应用程序是否在开发环境或生产环境中运行

在运行时更改Polymer应用程序的主题

在运行时更改应用程序主题

在运行时更改应用程序MainForm

在运行时更改应用程序语言

当 twilio 功能在后台运行时,如何继续使用我的网络应用程序?

在运行时在我的应用程序中添加数据源

当我尝试设置MapType时,MapFragment使应用程序在运行时崩溃

如何在运行时更改应用程序环境?

如何保护Flash应用程序免于在运行时更改系统时间

如何在运行时更改应用程序输出类型

如何在运行时更改android应用程序的名称和图标?

XAML-如何反转UIElement.RenderTransform?

如何找到单击的UIElement子级

如何查找由DataTemplate生成的UIElement

如何在WPF中找到UIElement?

如何使UIElement的位置转换持久化?

使用UIElement创建Cordova UWP插件

Windows应用商店应用程序:使用RenderTransform时,UIElement无法完全呈现