NSWindow with round corners and shadow

后端 未结 11 1854
爱一瞬间的悲伤
爱一瞬间的悲伤 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 03:11

    Update

    I realised old approach was not able to create precise round corner. So I updated example to make precise round corner.

            window1.backgroundColor             =   NSColor.whiteColor()
            window1.opaque                      =   false
            window1.styleMask                   =   NSResizableWindowMask
                                                |   NSTitledWindowMask
                                                |   NSFullSizeContentViewWindowMask
            window1.movableByWindowBackground   =   true
            window1.titlebarAppearsTransparent  =   true
            window1.titleVisibility             =   .Hidden
            window1.showsToolbarButton          =   false
            window1.standardWindowButton(NSWindowButton.FullScreenButton)?.hidden   =   true
            window1.standardWindowButton(NSWindowButton.MiniaturizeButton)?.hidden  =   true
            window1.standardWindowButton(NSWindowButton.CloseButton)?.hidden        =   true
            window1.standardWindowButton(NSWindowButton.ZoomButton)?.hidden         =   true
    
            window1.setFrame(CGRect(x: 400, y: 0, width: 400, height: 500), display: true)
            window1.makeKeyAndOrderFront(self)
    

    Here's full working example.


    Oudated

    Special treatment is not required at least in OS X 10.10.

    import Cocoa
    
    class ExampleApplicationController: NSObject, NSApplicationDelegate {
        class ExampleController {
    
            let window1 =   NSWindow()
            let view1   =   NSView()
    
            init(){
                window1.setFrame(CGRect(x: 400, y: 0, width: 400, height: 500), display: true)
                window1.contentView                 =   view1
    
                window1.backgroundColor             =   NSColor.clearColor()
                window1.opaque                      =   false
                window1.styleMask                   =   NSBorderlessWindowMask | NSResizableWindowMask
                window1.movableByWindowBackground   =   true
                window1.makeKeyAndOrderFront(self)
    
                view1.wantsLayer                =   true
                view1.layer!.cornerRadius       =   10
                view1.layer!.backgroundColor    =   NSColor.whiteColor().CGColor
    
                /// :ref:   http://stackoverflow.com/questions/19940019/nswindow-with-round-corners-and-shadow/27613308#21247949
                window1.invalidateShadow()  //  This manual invalidation is REQUIRED because shadow generation is an expensive operation.
            }
        }
    
        let example1    =   ExampleController()
    }
    

    You can download a working example from here.

提交回复
热议问题