objective-c - 使带有透明标题栏的 NSWindow 部分不可移动

标签 objective-c swift cocoa

我有一个带有分屏的 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 属性

注意:我查了一些东西herehere

关于objective-c - 使带有透明标题栏的 NSWindow 部分不可移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30155441/

相关文章:

ios - 在 Core Data 中设置实体之间关系的原因

ios - 成功登录后重新分配 RootViewController

ios - 设置 XlsxReaderWriter - Swift

ios - 无法使用 ADLilyTableView

xcode - 如何使用 AppleScriptObjC 获取 Spotify 应用程序的图像和轨道名称

objective-c - 为什么如果在 AppDelegate 中定义了一个属性,而它在 ViewController 中不可用?

swift - 当拉动刷新 cellForRowAt 方法中的 indexPath.row 时返回不正确的值

ios - 获取所有日历时出错 : Error Domain=EKCADErrorDomain Code=1013 "(null)" Swift 3

ios - superview.superview 上的透明 UILabel textColor(有点)

Objective-C 重用 NSString 内存泄漏