NSPopOver& NSViewController - 拖动以调整大小

时间:2015-08-04 10:45:35

标签: macos cocoa nswindow nsviewcontroller nspopover

我正在制作一个我使用的NSPopOver的MenuBar应用。问题是NSPopover uses NSViewController as a contentViewController,其大小得到修复。我的要求是使NSViewController的大小更加灵活,就像NSWindowController一样(设置最小大小和最大值取决于总屏幕大小)。简单来说,当用户拖动它时,如何更改NSViewController(NSPopOver)的大小。我是OS X编程的新手。

3 个答案:

答案 0 :(得分:7)

我终于通过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)

    }

这是我跟踪鼠标事件的方式。在鼠标拖动时,只计算当前位置和新位置(到用户拖动的位置),然后检查点是否小于弹出窗口的默认大小,即(450,650)。计算完一个点后,只需设置弹出窗口的大小即可。

这只是一种提议的方式。必须有比这更好的东西,但暂时这就是我所做的。

答案 1 :(得分:1)

这是一个只处理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)

     }

答案 2 :(得分:1)

我也有同样的需求,并且由于上面的有用评论,我创建了PopoverResize。这允许用户调整菜单栏NSPopover的大小。它也包括边缘的光标。

https://github.com/dboydor/PopoverResize