在菜单栏应用程序中拖动并调整我的NSPopover(swift)

时间:2017-10-08 12:25:15

标签: swift macos resize nspopover

我需要在菜单栏应用中调整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边框时,我希望光标变为箭头,我该如何实现呢?
  3. 编辑:

    我试过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

2 个答案:

答案 0 :(得分:0)

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

https://github.com/dboydor/PopoverResize

答案 1 :(得分:0)

这对我有用:

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 
}