4

I create a NSWindowController and NSViewController using Interface Builder, And then, i removed the NSWindow's titlebar so that I can custom Window. i create a class subclass NSWindow And do following things in class.

override var canBecomeKey: Bool {
    return true
}

override var canBecomeMain: Bool {
    return true
}

i also set these in NSWindowController:

{  
    self.window?.becomeKey()
    self.window?.isMovableByWindowBackground = true
    self.window?.isMovable = true;
    self.window?.acceptsMouseMovedEvents = true
}

from here, the custom Window is can be dragged,
But when i make the NSViewController as the NSWindowController's ContentViewController, i can not drag the customWindow.

What could be happening here?

A O
  • 5,516
  • 3
  • 33
  • 68
J.Wu
  • 41
  • 5

2 Answers2

0

I had this problem before, here was my solution that works (it's in objective-C though, so just throwing it out there in case it helps):

@property BOOL mouseDownInTitle;

- (void)mouseDown:(NSEvent *)theEvent
{
   self.initialLocation = [theEvent locationInWindow];
   NSRect windowFrame = [self frame];
   NSRect titleFrame = NSMakeRect(0, windowFrame.size.height-40, windowFrame.size.width, 40);
   NSPoint currentLocation = [theEvent locationInWindow];

   if(NSPointInRect(currentLocation, titleFrame))
   {
      self.mouseDownInTitle = YES;
   }
}

- (void)mouseDragged:(NSEvent *)theEvent
{
   if( self.mouseDownInTitle )
   {
      NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
      NSRect windowFrame = [self frame];

      NSPoint newOrigin = windowFrame.origin;
      NSPoint currentLocation = [theEvent locationInWindow];



      // Update the origin with the difference between the new mouse location and the old mouse location.
      newOrigin.x += (currentLocation.x - self.initialLocation.x);
      newOrigin.y += (currentLocation.y - self.initialLocation.y);

      // Don't let window get dragged up under the menu bar
      if ((newOrigin.y + windowFrame.size.height) > (screenVisibleFrame.origin.y + screenVisibleFrame.size.height)) {
         newOrigin.y = screenVisibleFrame.origin.y + (screenVisibleFrame.size.height - windowFrame.size.height);
      }

      [self setFrameOrigin:newOrigin];
   }
}
A O
  • 5,516
  • 3
  • 33
  • 68
  • i have already used the method that you advised , but it is not work for me. when i drag the view, it will be disappear, i thought there maybe some wrong with my dragged view's frame set. – J.Wu Nov 28 '16 at 07:01
0

The controlling factor here is the view's mouseDownCanMoveWindow property. By default that depends, in turn, on its isOpaque property. If you or one of your superclasses override isOpaque to return true, then the default implementation of mouseDownCanMoveWindow will return false. If you want it to behave differently, then you have to override that, too.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • I am already find my problem, when i drag , what i actually drag is NSViewController's view, i make the window become invisible, i thought it is window, but it is not. i am still confused that i add view on the custom window By xib, it is work, But add a Controller's view on the custom window, it is not work – J.Wu Nov 28 '16 at 06:43