Resizing NSWindow to match view controller size in storyboard

Kaunteya picture Kaunteya · Dec 20, 2014 · Viewed 9.4k times · Source

I am working on Xcode 6.1.1 on OSX 10.10. I am trying out storyboards for Mac apps. I have a NSTabViewController using the new NSTabViewControllerTabStyleToolbar tabStyle and it is set as the default view controller for the window controller. How do I make my window resize according to the current selected view controller?

Is it possible to do entirely in Interface Builder? Here is what my storyboard looks like: storyboard

Answer

rougeExciter picture rougeExciter · Mar 30, 2015

The auto layout answer is half of it. You need to set the preferredContentSize in your ViewController for each tab to the fitting size (if you wanted the tab to size to the smallest size satisfying all constraints).

override func viewWillAppear() {
        super.viewWillAppear()
        preferredContentSize = view.fittingSize
}

If your constraints are causing an issue below try first with a fixed size, the example below sets this in the tab item's view controller's viewWillAppear function (Swift used here, but the Objective-C version works just as well).

override func viewWillAppear() {
        super.viewWillAppear()
        preferredContentSize = NSSize(width: 400, height: 280)
}

If that works, fiddle with your constraints to figure out what's going on