在 OS X Yosemite 中,Apple 引入了一个新的类NSVisualEffectView
. 目前,这个类没有文档,但我们可以在 Interface Builder 中使用它。
如何NSVisualEffectView
在窗口的标题栏中使用?
下面是示例:在 Safari 中,当您滚动时,内容会出现在工具栏和标题栏下方,并带有活力和模糊效果。
在 OS X Yosemite 中,Apple 引入了一个新的类NSVisualEffectView
. 目前,这个类没有文档,但我们可以在 Interface Builder 中使用它。
如何NSVisualEffectView
在窗口的标题栏中使用?
下面是示例:在 Safari 中,当您滚动时,内容会出现在工具栏和标题栏下方,并带有活力和模糊效果。
@sgonzalez 的回答迫使我探索NSWindow.h
找到titlebarAppearsTransparent
财产的文件。
所以我们得到:
class BluredWindow: NSWindow {
override func awakeFromNib() {
let visualEffectView = NSVisualEffectView(frame: NSMakeRect(0, 0, 300, 180))
visualEffectView.material = NSVisualEffectView.Material.dark
visualEffectView.blendingMode = NSVisualEffectView.BlendingMode.behindWindow
visualEffectView.state = NSVisualEffectView.State.active
self.styleMask = self.styleMask | NSFullSizeContentViewWindowMask
self.titlebarAppearsTransparent = true
//self.appearance = NSAppearance(named: NSAppearanceNameVibrantDark)
self.contentView.addSubview(visualEffectView)
self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[visualEffectView]-0-|",
options: NSLayoutConstraint.FormatOptions.directionLeadingToTrailing,
metrics: nil,
views: ["visualEffectView":visualEffectView]))
self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[visualEffectView]-0-|",
options: NSLayoutConstraint.FormatOptions.directionLeadingToTrailing,
metrics: nil,
views: ["visualEffectView":visualEffectView]))
您也可以在 IB 中设置 NSVisualEffectView ,它将在标题栏上展开。
您需要修改窗口的样式掩码以包含NSFullSizeContentViewWindowMask
,以便其内容视图可以“溢出”到其中。
您可以通过将此行添加到 AppDelegate 轻松完成此操作:
self.window.styleMask = self.window.styleMask | NSFullSizeContentViewWindowMask;
如果你想让它看起来很暗,就像在 FaceTime 中一样,你还需要添加这行代码:
self.window.appearance = NSAppearance(named: NSAppearanceNameVibrantDark)
http://eon.codes/blog/2016/01/23/Chromeless-window/
NSFullSizeContentViewWindowMask
(这样半透明也将在标题栏区域可见,将其省略,标题栏区域将为空白)self.titlebarAppearsTransparent = true
(隐藏标题栏默认图形).
let visualEffectView = NSVisualEffectView(frame: NSMakeRect(0, 0, 0, 0))//<---the width and height is set to 0, as this doesn't matter.
visualEffectView.material = NSVisualEffectMaterial.AppearanceBased//Dark,MediumLight,PopOver,UltraDark,AppearanceBased,Titlebar,Menu
visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow//I think if you set this to WithinWindow you get the effect safari has in its TitleBar. It should have an Opaque background behind it or else it will not work well
visualEffectView.state = NSVisualEffectState.Active//FollowsWindowActiveState,Inactive
self.contentView = visualEffectView/*you can also add the visualEffectView to the contentview, just add some width and height to the visualEffectView, you also need to flip the view if you like to work from TopLeft, do this through subclassing*/