2016-11-26 40 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,它是工作,但在自定義窗口中添加一個控制器的視圖,它不工作 –