阴影在 "main" 个情节提要中绘制,但不在任何其他情节提要中绘制

Shadow is drawn in "main" Storyboard but not in any other storyboard

我尝试编写自己的组合框,因为 NSComboBox 没有我需要的功能。

所以,我子class编辑了一个 NSTextField 并监视文本输入,并且根据字符串值,TableView 将获取它的数据。

所以当应该显示 TableView 时,TextFields 超级视图将添加 NSScrollView 并调整它的高度等。 所有这些都很好。

DropShadow 不太顺利。

因此,我创建了一个新的 Xcode-Project,并在 main.Storyboard 中添加了一个 NSTextField,并将 class 更改为我的 Subclass。 只要我输入文本,TableView 就会出现并且有一个 dropShadow。

接下来我创建一个新的故事板,添加一个新的 WindowController 并执行与 main.Storyboard 中相同的步骤:添加一个 NSTextField,将 class 更改为我的 subclass。 现在,我在 main.Storyboard 中添加了一个按钮,它具有呈现新 Storyboard 的操作。

在新的故事板中,textfield 和 tableView 的行为符合预期,只是 TableView/ScrollView 没有任何阴影。

即使我将 Generals 选项卡中的 MainInterface 更改为新的 Storyboard,TableView 也没有 DropShadow。

IB 中两个故事板的设置看起来相同。

那么,有什么提示可以解决这个问题吗?

添加和显示scrollView的代码如下:

self.scrollView = NSScrollView(frame: NSRect.zero)
self.scrollView!.documentView = self.tableView
self.scrollView!.translatesAutoresizingMaskIntoConstraints = false
self.scrollViewHeightConstraint = NSLayoutConstraint(item: self.scrollView!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100)
let topConstraint = NSLayoutConstraint(item: self.scrollView!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let widthConstraint = NSLayoutConstraint(item: self.scrollView!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0)
let leadinghConstraint = NSLayoutConstraint(item: self.scrollView!, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)
superview.addSubview(self.scrollView!)
superview.addConstraints([self.scrollViewHeightConstraint!,topConstraint,widthConstraint,leadinghConstraint])

let dropShadow = NSShadow()
dropShadow.shadowColor = NSColor.darkGray
dropShadow.shadowOffset = NSMakeSize(0, 10.0)
dropShadow.shadowBlurRadius = 3.0

self.scrollView!.wantsLayer = true
self.scrollView!.shadow = dropShadow

经过快速测试……我找到了 "problem"。

对于新的 ViewControllers 视图,我必须设置

self.view.wantsLayer = true