How to use NSVisualEffectView to blend a window with a background

There seem to be a lot of questions for older versions of Swift / Xcode, but for some reason it did not work with the latest update. I created NSVisualEffectView, blurryView and added a subview to my main view:

class ViewController: NSViewController { @IBOutlet weak var blurryView: NSVisualEffectView! override func viewDidLoad() { super.viewDidLoad() //background styling blurryView.wantsLayer = true blurryView.blendingMode = NSVisualEffectBlendingMode.behindWindow blurryView.material = NSVisualEffectMaterial.dark blurryView.state = NSVisualEffectState.active self.view.addSubview(blurryView, positioned: NSWindowOrderingMode.above, relativeTo: nil) // Do any additional setup after loading the view. } ... } 

But when I run it, the effect does not affect the window. (when I set it inside the window and overlay it on top of another, the blur works correctly, but I want the window to blur.) I also tried to do the same in the App Delegate class, but I can't connect my window as an output and therefore, cannot add a blurry view to the window. This is what the code looks like:

 class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { // Insert code here to initialize your application blurryView.wantsLayer = true blurryView.blendingMode = NSVisualEffectBlendingMode.withinWindow blurryView.material = NSVisualEffectMaterial.dark blurryView.state = NSVisualEffectState.active self.window.contentView?.addSubview(blurryView) } ... } 

To get an idea of โ€‹โ€‹what I'm looking for: NVVisualEffectView Vibrancy

+1
source share
1 answer

This works pretty easy:

  • In the Builder interface, drag and drop NSVisualEffectView directly as the subzone of the main view of your scene.
  • In the Properties Inspector set the Blending Mode to Behind Window
  • Add the rest of the views you need as NSVisualEffectView NSVisualEffectView
  • To do this, you are done.

Here is an example:

enter image description here

Panel 1 View Controller is my blurry view, Background View is the first (not blurry) view in my โ€œrealโ€ view hierarchy.

+3
source

All Articles