Search code examples
macosstoryboardappkitnsviewcontrollernsshadow

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


I try to write my own comboBox as NSComboBox does not has features I need.

So, I subclassed an NSTextField and monitor the textinput and depending of the stringValue a TableView will get it's data.

So when the TableView should be displayed the TextFields superview will add the NSScrollView and adjust it's height and so on. All of that works fine.

What goes not so well is the DropShadow.

So, I create a new Xcode-Project an in the main.Storyboard add a NSTextField and change the class to my Subclass. As soon as I type text the TableView appears and has a the dropShadow.

Next I create a new Storyboard, add a new WindowController and do the same steps as in the main.Storyboard: Adding an NSTextField, change the class to my subclass. Now I add a Button in the main.Storyboard which has an action to present the new Storyboard.

In the new storyboard the textfield and tableView behave as expected except that the TableView/ScrollView does not have any shadow.

Even when I change MainInterface in the Generals tab to the new Storyboard, no DropShadow for the TableView.

The Settings in the IB for both Storyboards look equal.

So, any hint how I can fix this?

Here is the code for adding and displaying the 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

Solution

  • After a quick test … I found the "problem".

    For the new ViewControllers view I had to set

    self.view.wantsLayer = true