Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSVisualEffectView with rounded corners

How display a NSVisualEffectView with with rounded corners in OS X?

My code to add my NSVisualEffectView:

let visualEffectView = NSVisualEffectView(frame: NSMakeRect(0, 0, 300, 300))
visualEffectView.material = NSVisualEffectMaterial.Dark
visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
self.addSubview(visualEffectView)
like image 852
Geek20 Avatar asked Aug 17 '15 03:08

Geek20


2 Answers

You can enable layer backed views for your NSVisualEffectView by setting wantsLayer to true and then set the cornerRadius of the backing layer:

    let visualEffectView = NSVisualEffectView(frame: NSMakeRect(0, 0, 300, 300))
    visualEffectView.material = NSVisualEffectMaterial.Dark
    visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
    visualEffectView.wantsLayer = true
    visualEffectView.layer?.cornerRadius = 15.0
    self.view.addSubview(visualEffectView)

This results in a effect view with nice rounded corners:

enter image description here

like image 110
Thomas Zoechling Avatar answered Nov 02 '22 23:11

Thomas Zoechling


NSVisualEffectView has a maskImage property which you can use to clip the view to an arbitrary shape.

From the header:

/* The mask image masks this view. It is best to set this to the
   smallest mask image possible and properly set the image.capInsets to
   inform the image on how to stretch the contents when it is used as a
   mask. Setting the maskImage on an NSVisualEffectView that is the
   window.contentView will correctly set the window's shadow.
 */
public var maskImage: NSImage?

For example, you can use an NSImage with rounded corners and set its capInsets to the corner radius and its resizingMode to .stretch.

like image 45
sam Avatar answered Nov 03 '22 00:11

sam