【问题标题】:Drag and resize my NSPopover in menu bar app(swift)在菜单栏应用程序中拖动并调整我的 NSPopover 大小(swift)
【发布时间】:2017-10-08 12:25:15
【问题描述】:

我需要在菜单栏应用中调整 NSPopver 的大小。这是我的方法:

override func mouseDragged(with event: NSEvent) {
    let appDelegate : AppDelegate = NSApplication.shared().delegate as! AppDelegate
    let originSize = appDelegate.popover.contentSize

    let currentLocation = NSWindow().mouseLocationOutsideOfEventStream

    let delta_x = NSWindow().mouseLocationOutsideOfEventStream.x - currentLocation.x
    let delta_y = NSWindow().mouseLocationOutsideOfEventStream.y - currentLocation.y

    let newWidth = originSize.width + delta_x
    let newHeight = originSize.height + delta_y

    appDelegate.popover.contentSize = NSSize(width: newWidth, height: newHeight)
}

问题:

  1. NSPopover 调整大小操作无法准确执行。
  2. 我希望当光标悬停到 NSPopover 边框时光标变为箭头,我该如何实现?

编辑:

我已经尝试NSPopOver & NSViewController - Drag to resize解决方案:

here is the effect

这里是转换为 swift3 版本的代码:

override func mouseDragged(with event: NSEvent) {
    var currentLocation = NSEvent.mouseLocation()

    var newOrigin   = currentLocation
    let screenFrame = NSScreen.main()?.frame

    newOrigin.x     = screenFrame!.size.width - currentLocation.x
    newOrigin.y     = screenFrame!.size.height - currentLocation.y

    // Don't let window get dragged up under the menu bar
    if newOrigin.x < 260 {
        newOrigin.x = 260
    }

    if newOrigin.y < 300 {
        newOrigin.y = 300
    }


    let appDelegate : AppDelegate = NSApplication.shared().delegate as! AppDelegate
    appDelegate.popover.contentSize = NSSize(width: newOrigin.x, height: newOrigin.y)

}

This is what I expect to achieve

【问题讨论】:

  • 我试过他的解决方案,但是没有用,你能帮帮我吗?
  • 你能描述一下它到底是怎么不工作的,或者你的情况与那个有什么不同吗?
  • 你好,我编辑了我的问题,请给我一些建议。

标签: swift macos resize nspopover


【解决方案1】:

PopoverResize 允许用户调整菜单栏 NSPopover 的大小。它还包括边缘的光标。

https://github.com/dboydor/PopoverResize

【讨论】:

    【解决方案2】:

    这对我有用:

    override func mouseDragged(with event: NSEvent) {
        guard let appDelegate: AppDelegate = NSApplication.shared.delegate as? AppDelegate else { return }
        var size = appDelegate.statusItemManager.popover?.contentSize ?? CGSize.zero
        size.width += event.deltaX
        size.height += event.deltaY
        // Update popover size depend on your reference
        appDelegate.popover?.contentSize = size 
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-26
      • 1970-01-01
      • 2011-12-11
      • 2011-02-03
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 2018-06-23
      相关资源
      最近更新 更多