首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSPopOver & NSViewController -拖动以调整大小

NSPopOver & NSViewController -拖动以调整大小
EN

Stack Overflow用户
提问于 2015-08-04 10:45:34
回答 3查看 1.9K关注 0票数 5

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-07 10:10:23

我终于通过使用Mouse Events实现了它的工作。只是需要监视

代码语言:javascript
复制
override func mouseDown(theEvent: NSEvent) {}
override func mouseDragged(theEvent: NSEvent) {}

事件,并重置popOver的内容大小。希望有一天这会对某人有所帮助。

编辑

代码语言:javascript
复制
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)的默认大小小。计算完点后,只需设置弹出器的大小即可。

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

票数 11
EN

Stack Overflow用户

发布于 2018-10-29 18:53:40

我也有同样的需求,多亏了上面那些有用的评论,我创建了PopoverResize。这允许用户调整菜单NSPopover的大小。它还包括边缘的游标。

https://github.com/dboydor/PopoverResize

票数 3
EN

Stack Overflow用户

发布于 2016-12-31 21:12:25

下面是一个只处理垂直大小,而不是对NSPopover进行水平调整的答案。

代码语言:javascript
复制
    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)

     }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31807226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档