我有一个带有分屏的 NSWindow
,就像在 Reminders 中一样。因此我使用这段代码:
self.window.titlebarAppearsTransparent = true
self.window.styleMask |= NSFullSizeContentViewWindowMask
这非常有效。但是在窗口内,我有一个 SplitView(就像在 Reminders 应用程序中一样)和一个 NSOutlineView
在右侧。 OutlineView 上升到窗口角的顶部。
现在的问题是:在 OutlineView 的顶部单击并拖动会使窗口可移动。无论如何,我可以禁用它但仍然保持应用程序左侧的移动能力?
最佳答案
好的,您需要做两件事:
首先,您需要将窗口设置为不可移动。为此,将您的 Window 子类化并覆盖 isMovable
并返回 no。或者您调用 setMovable:
并将其设置为 no。
之后,您必须通过添加一个 View 来手动重新启用拖动,该 View 具有您要拖动的区域的确切大小和位置。或者,您可以设置一个 NSTrackingArea
。
无论哪种方式,您都需要重写 mouseDown:
并插入一些代码来移动窗口。
我在代码中的话:
Objective-C
[self.window setMovable:false];
// OR (in NSWindow subclass)
- (BOOL)isMovable {
return false;
}
//Mouse Down
- (void)mouseDown:(NSEvent *)theEvent {
_initialLocation = [theEvent locationInWindow];
NSPoint point;
while (1) {
theEvent = [[self window] nextEventMatchingMask: (NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
point =[theEvent locationInWindow];
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
NSRect windowFrame = [self.window frame];
NSPoint newOrigin = windowFrame.origin;
// Get the mouse location in window coordinates.
NSPoint currentLocation = point;
// Update the origin with the difference between the new mouse location and the old mouse location.
newOrigin.x += (currentLocation.x - _initialLocation.x);
newOrigin.y += (currentLocation.y - _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);
}
// Move the window to the new location
[self.window setFrameOrigin:newOrigin];
if ([theEvent type] == NSLeftMouseUp) {
break;
}
}
}
initialLocation
是一个 NSPoint
属性
关于objective-c - 使带有透明标题栏的 NSWindow 部分不可移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30155441/