NSPopOver和NSViewController - 拖动以调整大小

Nik*_*hla 4 macos cocoa nswindow nsviewcontroller nspopover

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

Nik*_*hla 8

我终于通过使用它了Mouse Events.只需要监控

override func mouseDown(theEvent: NSEvent) {}
override func mouseDragged(theEvent: NSEvent) {}
Run Code Online (Sandbox Code Playgroud)

事件,并重置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)

    }
Run Code Online (Sandbox Code Playgroud)

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

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


Dav*_*oyd 5

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

https://github.com/dboydor/PopoverResize