如何在OS X中显示带圆角的NSVisualEffectView?
我的代码添加我的NSVisualEffectView:
let visualEffectView = NSVisualEffectView(frame: NSMakeRect(0, 0, 300, 300))
visualEffectView.material = NSVisualEffectMaterial.Dark
visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
self.addSubview(visualEffectView)
答案 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)
这会产生具有漂亮圆角的效果视图:
答案 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
。