如何让一个按钮投射到 NSView 之外?

How to have a button which is projected outside the NSView?

我想实现一个投射到 NSView 之外的按钮。类似按钮如下图所示。

您可以通过确保您的父视图是图层支持并将其图层的 masksToBounds 属性 设置为 false 来实现这一点。

这是一个 AppKit playground,显示了这个动作:

//: A Cocoa based Playground to present user interface

import AppKit
import PlaygroundSupport

let nibFile = NSNib.Name("MyView")
var topLevelObjects : NSArray?

Bundle.main.loadNibNamed(nibFile, owner:nil, topLevelObjects: &topLevelObjects)
let views = (topLevelObjects as! Array<Any>).filter { [=10=] is NSView }

let rootView = views.first as! NSView

// create parent
let parentView = NSView()
rootView.addSubview(parentView)
parentView.wantsLayer = true
parentView.layer?.masksToBounds = false
parentView.layer?.backgroundColor = NSColor.red.cgColor
parentView.translatesAutoresizingMaskIntoConstraints = false
parentView.centerXAnchor.constraint(equalTo: rootView.centerXAnchor).isActive = true
parentView.centerYAnchor.constraint(equalTo: rootView.centerYAnchor).isActive = true
parentView.widthAnchor.constraint(equalToConstant: 200).isActive = true
parentView.heightAnchor.constraint(equalToConstant: 200).isActive = true

let childView = NSView()
parentView.addSubview(childView)
childView.wantsLayer = true
childView.layer?.backgroundColor = NSColor.green.cgColor
childView.translatesAutoresizingMaskIntoConstraints = false
childView.centerXAnchor.constraint(equalTo: parentView.leadingAnchor).isActive = true
childView.centerYAnchor.constraint(equalTo: parentView.centerYAnchor).isActive = true
childView.widthAnchor.constraint(equalToConstant: 100).isActive = true
childView.heightAnchor.constraint(equalToConstant: 100).isActive = true


// Present the view in Playground
PlaygroundPage.current.liveView = views[0] as! NSView

这将绘制:

绿色视图是红色视图的子视图。

如果您必须让视图溢出 window,则无法使用 CALayermasksToBound 属性 来实现。您需要使用 child 无边框 window 并正确定位。

这是一个示例(在 ObjC 中):

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application


    NSRect windowFrame = self.window.frame;
    NSRect childWindowFrame = {
        .origin.x = CGRectGetMidX(windowFrame) - 25,
        .origin.y = CGRectGetMinY(windowFrame) - 25,
        .size.width = 50,
        .size.height = 50,
    };

    NSWindow *childWindow = [[NSWindow alloc] initWithContentRect:childWindowFrame
                                                        styleMask:NSWindowStyleMaskBorderless
                                                          backing:NSBackingStoreBuffered
                                                            defer:YES];
    childWindow.backgroundColor = [NSColor clearColor];
    childWindow.contentView.wantsLayer = YES;
    childWindow.contentView.layer.backgroundColor = [NSColor redColor].CGColor;
    childWindow.contentView.layer.cornerRadius = 25.0;



    [self.window addChildWindow:childWindow ordered:NSWindowAbove];
}

这张截图中的红圈就是childwindow.