NSView如何显示阴影?

问题描述 投票:0回答:3

我在这里和其他博客中浏览了很多主题,但无法解决这个问题。我在窗口的内容视图中添加了一个子视图。这是故事板——

-

我已经将 customView 的出口拖到视图控制器,这里是视图控制器的代码 -

import Cocoa
import QuartzCore

class ViewController: NSViewController {

    @IBOutlet weak var customView: NSView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.wantsLayer = true
        self.customView.wantsLayer = true
        self.customView.layer?.backgroundColor = NSColor.redColor().CGColor
        self.customView.layer?.cornerRadius = 5.0
        self.customView.layer?.shadowOpacity = 1.0
        self.customView.layer?.shadowColor = NSColor.blackColor().CGColor
        self.customView.layer?.shadowOffset = NSMakeSize(0, -3)
        self.customView.layer?.shadowRadius = 20
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }
}

我在我的项目中添加了 QuartzCore 框架工作 -

但是没有出现阴影,这是屏幕截图 - .

我无法解决看似微不足道的问题。我错过了什么?感谢您的帮助。

macos swift cocoa calayer nsview
3个回答
36
投票

如果我添加以下行它解决了问题-

self.customView.shadow = NSShadow()

最终代码是-

import Cocoa
import QuartzCore

class ViewController: NSViewController {

    @IBOutlet weak var customView: NSView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.wantsLayer = true
        self.view.superview?.wantsLayer = true
        self.customView.wantsLayer = true
        self.customView.shadow = NSShadow()
        self.customView.layer?.backgroundColor = NSColor.redColor().CGColor
        self.customView.layer?.cornerRadius = 5.0
        self.customView.layer?.shadowOpacity = 1.0
        self.customView.layer?.shadowColor = NSColor.greenColor().CGColor
        self.customView.layer?.shadowOffset = NSMakeSize(0, 0)
        self.customView.layer?.shadowRadius = 20
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

我无法确定问题可能有人会指出来。


0
投票

我添加了如下代码以获得 NSView 的阴影。它对我有用。

     @IBOutlet weak var leftCustomBgView: NSView!
    
     override func viewDidAppear() {
        super.viewDidAppear()
        initialSetUp()
     }

    fileprivate func initialSetUp() {
        self.leftCustomBgView.wantsLayer = true
        self.leftCustomBgView.shadow = NSShadow()
        self.leftCustomBgView.layer?.backgroundColor = NSColor.white.cgColor
        self.leftCustomBgView.layer?.cornerRadius = 6
        self.leftCustomBgView.layer?.shadowColor = NSColor.black.cgColor
        self.leftCustomBgView.layer?.shadowOpacity = 1.0
        self.leftCustomBgView.layer?.shadowOffset = CGSize(width: 0, height: 0)
        self.leftCustomBgView.layer?.shadowRadius = 6
    }

----------------或----------------

self.leftCustomBgView.addShadow(customViewBgColor: NSColor.white,
                                    customViewRadius: 6,
                                    shadowViewColor: NSColor.black,
                                    shadowViewRadius: 6,
                                    opacity: 1.0)

extension NSView {
//Add Shadow to NSView
func addShadow(customViewBgColor: NSColor, customViewRadius: CGFloat, shadowViewColor: NSColor, shadowViewRadius: CGFloat, opacity: Float) {
    self.wantsLayer = true
    self.shadow = NSShadow()
    self.layer?.backgroundColor = customViewBgColor.cgColor
    self.layer?.cornerRadius = customViewRadius
    self.layer?.shadowColor = shadowViewColor.cgColor
    self.layer?.shadowOpacity = opacity
    self.layer?.shadowOffset = CGSize(width: 0, height: 0)
    self.layer?.shadowRadius = shadowViewRadius
 }
}

0
投票

接受的答案的替代方案,您可以在分配之前配置

NSShadow

文档说,正如一些人已经指出的那样:

如果视图没有层,设置这个属性的值没有效果。

例子

let shadow = NSShadow()
shadow.shadowColor = .red
shadow.shadowBlurRadius = 20
        
let view = NSView()
view.wantsLayer = true
view.shadow = shadow
© www.soinside.com 2019 - 2024. All rights reserved.