NSPopOver和NSViewController-拖动以调整大小 [英] NSPopOver & NSViewController - Drag to resize

查看:450
本文介绍了NSPopOver和NSViewController-拖动以调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作要使用NSPopOver的MenuBar应用.问题是NSPopover uses NSViewController as a contentViewController的大小固定.我的要求是使NSViewController的尺寸灵活,即像NSWindowController一样(设置最小尺寸和最大尺寸取决于总屏幕尺寸).简而言之,当用户拖动时如何更改NSViewController(NSPopOver)的大小.我是OS X编程的新手.

I am making a MenuBar app for which I am using NSPopOver. The problem is that NSPopover uses NSViewController as a contentViewController, whose size gets fixed. My requirement is to make the size of NSViewController flexible i.e just like NSWindowController(set the minimum size and maximum depends upon the total screen size). In simple words how to change the size of NSViewController(NSPopOver) when user drags it. I am new to OS X programming.

推荐答案

我终于通过使用Mouse Events使它工作了.只需监视

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

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

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

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

编辑

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).计算完点后,只需设置弹出框的大小即可.

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.

这篇关于NSPopOver和NSViewController-拖动以调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆