swift - 如何使用 NSVisualEffectView 将窗口与背景混合

标签 swift macos cocoa swift3

对于旧版本的 Swift/Xcode 似乎有很多问题,但由于某种原因它不能与最新更新一起使用。我创建了一个 NSVisualEffectView、blurryView,并将 subview 添加到我的主视图中:

class ViewController: NSViewController {
    @IBOutlet weak var blurryView: NSVisualEffectView! 
    override func viewDidLoad() {
        super.viewDidLoad()

        //background styling
        blurryView.wantsLayer = true
        blurryView.blendingMode = NSVisualEffectBlendingMode.behindWindow
        blurryView.material = NSVisualEffectMaterial.dark
        blurryView.state = NSVisualEffectState.active

        self.view.addSubview(blurryView, positioned: NSWindowOrderingMode.above, relativeTo: nil)

        // Do any additional setup after loading the view.
    }
...
}

但是当我运行它时,对窗口没有任何影响。 (当我将它设置在窗口内,并将它放在我的另一个 View 之上时,模糊效果正常,但我只希望窗口模糊。)我也尝试在我的 App Delegate 类中做同样的事情,但我可以'将我的窗口连接为 socket ,因此无法将模糊 View 添加到窗口。代码如下所示:

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application

        blurryView.wantsLayer = true
        blurryView.blendingMode = NSVisualEffectBlendingMode.withinWindow
        blurryView.material = NSVisualEffectMaterial.dark
        blurryView.state = NSVisualEffectState.active

        self.window.contentView?.addSubview(blurryView)

    }
...
}

了解我是否正在寻找:NSVisualEffectView Vibrancy

最佳答案

它工作起来很简单:

  1. 在 Interface Builder 中,直接拖动一个 NSVisualEffectView 作为场景主视图的 subview 。
  2. Properties Inspector 中将 Blending Mode 设置为 Behind Window
  3. 将您需要的其余 View 添加为 NSVisualEffectView
  4. 的 subview
  5. 就是这样,你完成了

这是一个例子:

enter image description here

Panel 1 View Controller 是我的模糊 View ,Background View 是我“真实” View 层次结构中的第一个(非模糊) View 。

关于swift - 如何使用 NSVisualEffectView 将窗口与背景混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41475551/

相关文章:

objective-c - 如何构建一个 Objective-C 静态库?

c++ - Mac OS X : is it possible to imbue a non-main thread to become "The Main Thread" of a process?

objective-c - Objective-C 中 @property 声明中的 "getter"关键字?

ios - 如何为 URLSessionDataTask 分配委托(delegate)?

ios - Alamofire 响应回调中的委托(delegate)为 nil

ios - 无法将类型 'UITableViewCell' 的值转换为 'SwipeCellKit.SwipeTableViewCell'

python - 在 OSX 上使用 python 通过 ODBC 连接到数据库

macos - 如何终止/重置捆绑 XPC 助手?

swift - 从 Realm 结果 Swift 中将 Int 转换为字符串

macos - 如何在 macOS 上向客户展示具有附加服务器模式的应用程序