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

标签 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 边框时光标变为箭头,如何实现?

编辑:

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

最佳答案

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

https://github.com/dboydor/PopoverResize

关于swift - 在菜单栏应用程序中拖动并调整我的 NSPopover 大小(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46631080/

相关文章:

objective-c - 静音 Cocoa 错误提示音

html - 如何在调整可调整大小的 div 时获取 ScaleX 和 ScaleY 值..?

swift - Swift 3 中的 UIView isKindOfClass

ios - AVKit – 视频剪辑仅循环 2 次

c - 无法编译 C 代码(开发人员工具、OS X)

java - 为什么 SetMinimumSize 设置最小高度而不是宽度?

vb.net - 调整图像大小并保持纵横比

swift - 如何为多个按钮设置相同的 IBAction?

Swift:从 Firebase 数据库检索数据以进行标签

cocoa - 是否可以在 OSX 中使用自由形状作为窗口?