Search code examples
objective-cmacoscocoaautolayoutnslayoutconstraint

Autolayout is not working with NSSplitView and NSPageController with Storyboard. Is this Apple Bug?


I just created a empty project on github --> here <-- to demonstrate the problem (Done in objective-c)

The project is a simple storyboard project. An NSWindowController loads an NSPageController which loads a NSSplitView containing 3 panes. There is no code in the sample project, except the code to load the screens. When the project runs, it looks like this enter image description here.

How do I get the constraints to make the splitView stretch all the way to the ends when the window is resized? The weird thing is, if you Switch the NSWindowController's contentController from the NSPageController to the NSSplitViewController, then every thing works as expected. Is this Apple Bug? I would appreciate any answer swift/objectivec please. I've tried but nothing works.

[EDIT] - Based on the answer below and further researching (contacted Apple), it appears that NSPageViewController does not use autolayout constraints but relies on autoresizing mask and frame setting on its children views.

So when the page controller creates its view controllers, we should set this:

-(NSViewController *)pageController:(NSPageController *)pageController viewControllerForIdentifier:(NSString *)identifier {
    NSViewController *viewController = [self.storyboard instantiateControllerWithIdentifier:identifier];
    [viewController.view setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
    return viewController;
}

With this, the problem is fixed. I wish as an update in future, this control works with Autolayout constraints as it seems more natural.


Solution

  • I had many problems with NSPageController. I found that the solution is to not use auto layout with it.

    Try to use the NSAutoresizingMaskOptions on your NSSplitView. First, remove all constraints inside the NSPageController.

    Then:

    splitView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
    splitView.frame = pageController.view.bounds;
    

    or

    splitView.autoresizingMask = [.ViewWidthSizable, .ViewHeightSizable]
    splitView.frame = pageController.view.bounds
    

    EDIT

    Made a project based on yours here