OpenThread
OpenThread

Reputation: 2094

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

Upvotes: 9

Views: 1438

Answers (1)

OpenThread
OpenThread

Reputation: 2094

Create an subclass of NSImageView and override mouseDownCanMoveWindow method:

- (BOOL)mouseDownCanMoveWindow
{
    return YES;
}

Upvotes: 17

Related Questions