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) {
        } // Move all subviews into the new effect view
        self.window.isMovableByWindowBackground = true
        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

Leave a Reply

Your email address will not be published. Required fields are marked *