2016-11-26 38 views
3

我使用Interface Builder创建了NSWindowControllerNSViewController,然后,我删除了NSWindow's标题栏,以便我可以自定义Window。我创建了一个类子类NSWindow并在课堂上做下列事情。无法拖动或移动自定义NSWindow

override var canBecomeKey: Bool { 
    return true 
} 

override var canBecomeMain: Bool { 
    return true 
} 

我还设置这些在NSWindowController

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

从这里,自定义窗口是可以拖动,
但是,当我让NSViewControllerNSWindowController'sContentViewController,我不能拖customWindow

这里可能会发生什么?

回答

0

我这个问题之前,这里是我的解决方案,它的工作原理(这是在Objective-C的,所以只是把它扔在那里的情况下,它可以帮助):

@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]; 
    } 
} 
+0

我已经使用了你建议的方法,但它对我来说不起作用。当我拖动视图时,它会消失,我认为我的拖动视图的框架集可能存在一些问题。 –

0

这里的控制因素是视图的mouseDownCanMoveWindow财产。默认情况下,依次取决于其isOpaque属性。如果您或您的某个超类覆盖isOpaque以返回true,则默认实现mouseDownCanMoveWindow将返回false。如果你希望它有不同的表现,那么你也必须重写它。

+0

我已经发现我的问题,当我拖动,我实际上拖动的是NSViewController的视图,我让窗口变得不可见,我认为它是窗口,但它不是。我仍然困惑,我在自定义窗口添加视图通过xib,它是工作,但在自定义窗口中添加一个控制器的视图,它不工作 –