Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSSplitViewController based application almost never launches with the correct size

I have this app that uses a NSSplitViewController as the root and has a NSTabViewController connected as its detailViewController.

This app is set to launch at 1024x768. The left pane should launch at 320x768 and the right pane (where the tabViewController is), should launch at 704x768.

From 10 times I run this app, 9 times it will launch with the incorrect size (about 500x500). Other strange thing is that this app should not be scalable, but if you hover the mouse near the window border you see cursor indication to scale.

I want this to launch at the correct size and have no scalable option.

Both of these settings are on interface builder but are being ignored.

You can download a sample project that demonstrates the problem, here. Stop and run the project several times to see the problem.

How do I solve this?

like image 658
Duck Avatar asked Jan 28 '15 10:01

Duck


2 Answers

I couldn't say for sure what's causing the problem, but one way you may be able to solve it is to add some constraints. Interface Builder doesn't allow you to constrain the default NSView instances that it inserts into the left and right panels of the split view, so you'll need to add your own. The screen-shot below is taken from your demo, but after I've done the following:

  1. Added a subview to the left split (My Content View), and pinned it's edges to the edges of its superview (the view Xcode automatically adds to the splitview)
  2. Added an explicit width constraint of 320 pixels to My Content View

enter image description here

When I load the app both splits are visible, the divider doesn't budge, and the window can't be resized.

Update - a better solution

Although constraints are one way to solve this problem, I think the root of the problem lies in a bit of unexpected behaviour in Interface Builder. When you drag an NSSplitViewController object onto the canvas, and make it the target of the window controller's content window relationship, the split-view controller's view outlet is not actually set. One consequence of this appears to be that, when you load the app, the divider will appear to be right over to one side. To resolve this, set the aforementioned view outlet to point at the split view:

enter image description here

I've created a demo project with a setup similar to that in the questioner's demo app.

like image 123
Paul Patterson Avatar answered Dec 07 '22 00:12

Paul Patterson


For reference, the same problem occurs if the window content segue points to an NSTabViewController scene. New windows open with a size of 500x500.

I solved it by placing a plain view controller with a container view between my window and my main tab view controller. The window will then use the size of the container view as initial size.

Here is what I did in detail:

  1. Added a new view controller scene to the storyboard
  2. Made that view the size I want my window to use initially
  3. Added a container view to the new view controller scene & added 4 constraints to have the container cover the view completely
  4. Connected the window's content segue to the new view controller
  5. Finally connect the container view to my actual tab view controller scene

Before:

[Window Controller Scene] → [Tab View Controller]

After:

[Window Controller Scene] → [View Controller Scene] → [Tab View Controller]
                             (with Container View)
like image 33
Mark Avatar answered Dec 06 '22 23:12

Mark