带圆角的NSVisualEffectView

时间:2015-08-17 03:32:36

标签: xcode macos swift cocoa nsvisualeffectview

如何在OS X中显示带圆角的NSVisualEffectView?

我的代码添加我的NSVisualEffectView:

let visualEffectView = NSVisualEffectView(frame: NSMakeRect(0, 0, 300, 300))
visualEffectView.material = NSVisualEffectMaterial.Dark
visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
self.addSubview(visualEffectView)

2 个答案:

答案 0 :(得分:7)

您可以通过将NSVisualEffectView设置为wantsLayer,然后设置支持层的true来为cornerRadius启用图层支持的视图:

    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)

这会产生具有漂亮圆角的效果视图:

enter image description here

答案 1 :(得分:1)

NSVisualEffectView具有maskImage属性,您可以使用该属性将视图剪切为任意形状。

从标题:

/* 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?

例如,您可以使用带圆角的NSImage并将其capInsets设置为角半径,将resizingMode设置为.stretch