我需要在菜单栏应用中调整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)
}
问题:
编辑:
我试过NSPopOver & NSViewController - Drag to resize解决方案:
以下是转换为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)
}
答案 0 :(得分:0)
PopoverResize,允许用户调整菜单栏NSPopover的大小。它还包括边缘的光标。
答案 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
}