NSPopOver & NSViewController - Drag to resize

﹥>﹥吖頭↗ 提交于 2019-12-05 16:16:10

I finally got it working by using Mouse Events. Just need to monitor the

override func mouseDown(theEvent: NSEvent) {}
override func mouseDragged(theEvent: NSEvent) {}

events, and reset the content size of the popOver. Hope this would be helpful to someone one day.

EDIT

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)

    }

This is how i tracked the mouse event. On Mouse drag just calculated the current position and the new position(To the point where user has dragged), then checked if the point is smaller then my default size for the Popover i.e. (450, 650) in this case. Once the point has been calculated, just set the size of the popover.

This is just a proposed way. There must be something better then this, but for time being this is what I did.

Here is an answer that handles only vertical, but not horizontal resizing of an 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)

     }

I had the same need, and thanks to the helpful comments above, I created PopoverResize. This allows a user to resize a menubar NSPopover. It includes cursors for the edges as well.

https://github.com/dboydor/PopoverResize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!