15

在 OS X Yosemite 中,Apple 引入了一个新的类NSVisualEffectView. 目前,这个类没有文档,但我们可以在 Interface Builder 中使用它。

如何NSVisualEffectView在窗口的标题栏中使用?

下面是示例:在 Safari 中,当您滚动时,内容会出现在工具栏和标题栏下方,并带有活力和模糊效果。

在此处输入图像描述

4

3 回答 3

19

@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 ,它将在标题栏上展开。

于 2014-06-27T04:40:35.773 回答
18

您需要修改窗口的样式掩码以包含NSFullSizeContentViewWindowMask,以便其内容视图可以“溢出”到其中。

您可以通过将此行添加到 AppDelegate 轻松完成此操作:

self.window.styleMask = self.window.styleMask | NSFullSizeContentViewWindowMask;

如果你想让它看起来很暗,就像在 FaceTime 中一样,你还需要添加这行代码:

self.window.appearance = NSAppearance(named: NSAppearanceNameVibrantDark)
于 2014-06-26T20:31:47.157 回答
9

带示例的分步教程:

http://eon.codes/blog/2016/01/23/Chromeless-window/

设置半透明:

  1. 将您的 NSWindow 子类的 styleMask 设置为NSFullSizeContentViewWindowMask(这样半透明也将在标题栏区域可见,将其省略,标题栏区域将为空白)
  2. 设置self.titlebarAppearsTransparent = true(隐藏标题栏默认图形)
  3. 将下面的代码添加到您的 NSWindow 子类中:(您现在应该有一个半透明的窗口)

.

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

在此处输入图像描述

还允许您创建半透明的无铬窗口:

在此处输入图像描述

于 2016-01-24T08:16:08.233 回答