NSWindow with round corners and shadow

后端 未结 11 1869
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 02:26

I\'m trying to crate a NSWindow without title bar (NSBorderlessWindowMask) with round corners and a shadow, similar to the below \"Welcome

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 02:46

    Here's a solution that can be done just with Interface Builder and two lines of extra code:

    Create a NIB file in Interface Builder containing your window.
    Configure it as shown below:

    Add a box (NSBox) object to the window content view in Interface Builder and make it fill the entire content view. Set border style, border thickness, border color, fill color and corner radius of the box as desired:

    Add all your other UI content to the box, treat is as if it was your window content view.

    In your code, create a subclass of NSWindowController which loads this NIB file and thus becomes the owner. Either in -awakeFromNib or -windowDidLoad, add the following code:

        self.window.opaque = NO;
        self.window.backgroundColor = NSColor.clearColor;
    

    That's all folks.
    No layers, no custom drawing code, no NSBezierPath, no NSShadow.
    Verified to work correctly on all systems from macOS 10.9 to 10.15

提交回复
热议问题