我正在制作一个MenuBar应用程序,我正在使用NSPopOver。问题是NSPopover uses NSViewController as a contentViewController,其大小是固定的。我的要求是使NSViewController的大小变得灵活,例如,就像NSWindowController一样(设置最小大小和最大值取决于总屏幕大小)。简单地说,当用户拖动NSViewController(NSPopOver)时,如何更改它的大小。我是OS编程的新手。
发布于 2015-08-07 10:10:23
我终于通过使用Mouse Events实现了它的工作。只是需要监视
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)的默认大小小。计算完点后,只需设置弹出器的大小即可。
这只是一种提议的方式。一定有比这更好的东西,但暂时我就是这样做的。
发布于 2018-10-29 18:53:40
我也有同样的需求,多亏了上面那些有用的评论,我创建了PopoverResize。这允许用户调整菜单NSPopover的大小。它还包括边缘的游标。
发布于 2016-12-31 21:12:25
下面是一个只处理垂直大小,而不是对NSPopover进行水平调整的答案。
override func mouseDragged(with theEvent: NSEvent) {
let currentLocation = NSEvent.mouseLocation()
let screenFrame = NSScreen.main()?.frame
var newY = screenFrame!.size.height - currentLocation.y
if newY < MIN_HEIGHT {
newY = MIN_HEIGHT
}
if newY > MAX_HEIGHT {
newY = MAX_HEIGHT
}
let appDelegate : AppDelegate = NSApplication.shared().delegate as! AppDelegate
appDelegate.popover.contentSize = NSSize(width: FIXED_WIDTH, height: newY)
}https://stackoverflow.com/questions/31807226
复制相似问题