我使用Interface Builder创建了NSWindowController
和NSViewController
,然后,我删除了NSWindow's
标题栏,以便我可以自定义Window。我创建了一个类子类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
}
从这里开始,可以拖动自定义窗口,NSViewController
作为NSWindowController's
ContentViewController
时,我无法拖动customWindow
。
这里可能会发生什么?
答案 0 :(得分: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];
}
}
答案 1 :(得分:0)
此处的控制因素是视图的mouseDownCanMoveWindow
属性。默认情况下,依赖于其isOpaque
属性。如果您或您的某个超类重写isOpaque
以返回true,则mouseDownCanMoveWindow
的默认实现将返回false。如果你希望它的行为不同,那么你也必须覆盖它。