gpt4 book ai didi

macos - 如何为 NSView 显示阴影?

转载 作者:搜寻专家 更新时间:2023-10-30 21:49:59 25 4
gpt4 key购买 nike

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

enter image description here -

我已经将 customView 的导出拖到 View Controller ,这里是 View Controller 的代码 -

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 框架 - enter image description here

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

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

最佳答案

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

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.
}
}


}

我无法确定问题,也许这里有人会指出。

关于macos - 如何为 NSView 显示阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32859617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com