首页 > 解决方案 > 更改 NSVisualEffectView 的颜色

问题描述

我目前正在尝试更改 NSVisualEffectView 的色调。这是我到目前为止得到的:

class MainViewViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // create a colored view to tint the visual effect view
        let coloredView = NSView(frame: self.view.frame)
        coloredView.layer?.backgroundColor = NSColor(red: 1, green: 0, blue: 0, alpha: 0.5).cgColor

        // create the visual effect view for the background blur
        let visualEffectView = NSVisualEffectView(frame: self.view.frame)
        visualEffectView.addSubview(coloredView)
        // add the visual effect view
        self.view.addSubview(visualEffectView, positioned: NSWindow.OrderingMode.below, relativeTo: nil)

    }
}

如您所见,我尝试添加另一个图层来为视觉效果视图着色。然而这并没有奏效。

有没有机会改变 NSVisualEffectView 的颜色?

标签: swiftcocoaappkit

解决方案


您不会更改视觉效果视图的“颜色”,因为它没有颜色。视觉效果视图根据视图的材质和混合模式混合背景和前景(子视图)。“背景”要么是窗口内视觉效果视图下的内容,要么是窗口后面的内容。

在这些示例中,在红色 NSBox 中的视觉效果视图中有一个蓝色 NSBox。

这个视觉效果视图的混合模式是“后”窗口。请注意,您可以从窗口后面看到绿色作为视觉效果视图中的混合背景。 在后面

此视觉效果视图的混合模式是“窗口内”。请注意,您会看到红色框,它是视觉效果视图的背景。 内

在这两种情况下,材料的外观都很“轻”。不同的材料会有不同的外观。

没有通用的方法可以让视觉效果视图创建“模糊的彩色背景”。它旨在根据材料/混合模式组合以特定方式使用,并且每种方式都有不同的外观。


推荐阅读