我正在制作一个使用 NSPopOver
的 MenuBar 应用程序。问题是 NSPopover 使用 NSViewController 作为 contentViewController,其大小是固定的。我的要求是使 NSViewController 的大小灵活,即就像 NSWindowController 一样(设置最小尺寸和最大尺寸取决于总屏幕尺寸)。简而言之,当用户拖动 NSViewController(NSPopOver) 时如何更改它的大小。我是 OS X 编程新手。
最佳答案
我终于通过使用鼠标事件
让它工作了。只需要监控
override func mouseDown(theEvent: NSEvent) {}
override func mouseDragged(theEvent: NSEvent) {}
事件,并重置popOver的内容大小。 希望有一天这会对某人有所帮助。
编辑
override func mouseDragged(theEvent: NSEvent) {
var currentLocation = NSEvent.mouseLocation()
println("Dragged at : \(currentLocation)")
var newOrigin = currentLocation
let screenFrame = NSScreen.mainScreen()?.frame
var windowFrame = self.view.window?.frame
newOrigin.x = screenFrame!.size.width - currentLocation.x
newOrigin.y = screenFrame!.size.height - currentLocation.y
println("the New Origin Points : \(newOrigin)")
// Don't let window get dragged up under the menu bar
if newOrigin.x < 450 {
newOrigin.x = 450
}
if newOrigin.y < 650 {
newOrigin.y = 650
}
println("the New Origin Points : \(newOrigin)")
let appDelegate : AppDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
appDelegate.popover.contentSize = NSSize(width: newOrigin.x, height: newOrigin.y)
}
这就是我跟踪鼠标事件的方式。在鼠标拖动时,仅计算当前位置和新位置(到用户拖动的点),然后检查该点是否小于我的 Popover 的默认大小,即本例中的(450, 650)。计算出点后,只需设置弹出框的大小即可。
这只是一个建议的方法。一定有比这更好的东西,但目前这就是我所做的。
关于macos - NSPopOver 和 NSViewController - 拖动以调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31807226/