Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drag-move NSWindow with a NSImageView on its contentView?

I'm creating a mini window like the iTunes mini window:

enter image description here

It's draggable and have a background image on it.

Then I created a NSWindow's subclass and overrides its initWithContentRect:styleMask:backing:defer: method, and added an NSImageView to it:

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
    contentRect.size = CGSizeMake(287, 287);
    self = [super initWithContentRect:contentRect styleMask:aStyle backing:bufferingType defer:flag];
    if (self)
    {
        [self setMovableByWindowBackground:YES];
        [self setOpaque:NO];
        [self setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]];

        NSImageView *imageView = [[NSImageView alloc] initWithFrame:(CGRect){CGPointZero, contentRect.size}];
        imageView.image = [NSImage imageNamed:@"MiniWindow.png"];
        [self.contentView addSubview:imageView];
    }
    return self;
}

But after I add the NSImageView to window's contentView, the window become undraggable.

How to make the window become draggable again?

Best regards

like image 265
OpenThread Avatar asked Nov 08 '13 03:11

OpenThread


1 Answers

Create an subclass of NSImageView and override mouseDownCanMoveWindow method:

- (BOOL)mouseDownCanMoveWindow
{
    return YES;
}
like image 102
OpenThread Avatar answered Oct 04 '22 19:10

OpenThread