Posts in Code

NSAlert with NSVisualEffectView

July 17th, 2019 Posted by Code 0 thoughts on “NSAlert with NSVisualEffectView”

Hello, I have extended the NSAlert class to replace the view with a transparent dark visual effect view.

extension NSAlert {
    func addVisualEffectView() {
        let EffectView = NSVisualEffectView(frame: self.window.frame)
        EffectView.material = .popover
        EffectView.blendingMode = .behindWindow
        EffectView.state = .active
        for (subview) in (self.window.contentView!.subviews) {
            EffectView.subviews.append(subview)
        } // Move all subviews into the new effect view
        self.window.styleMask.insert(.fullSizeContentView)
        self.window.isMovableByWindowBackground = true
        self.window.styleMask.insert(.borderless)
        self.window.contentView!.subviews.removeAll() // Remove all of the existing subviews
        self.window.titlebarAppearsTransparent = true
        self.window.titleVisibility = .hidden
        self.window.contentView = EffectView
        self.window.contentView!.wantsLayer = true
        self.window.contentView!.layer!.cornerRadius = 10.0
        self.window.backgroundColor = .clear
        self.window.isOpaque = false
    }
    
}