Search code examples
macosswiftdraggable

How to drag an image view within a window using swift (OS X not IOS)


I am creating a Mac application but having difficulty when dragging a window with an image on the view. Basically I can drag the main window but unable to drag the window when clicking and dragging the image view.

I can enable dragging on the main window with "window.movableByWindowBackground = true" seems to work but unable to get this working when dragging the image view on the window.

enter image description here

Any help would be greatly appreciated.


Solution

  • The trick is to subclass your NSImageView and override the mouseDownCanMoveWindow property:

    class MyImageView: NSImageView {
    
        override var mouseDownCanMoveWindow:Bool {
            return true
        }
    
    }