Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically grow document view of NSScrollView using auto layout?

Is there a simple way to get an NSScrollView to adapt to its document view changing size when using auto layout?

I have tried to call both setNeedsUpdateConstraints: and setNeedsLayout: on the document view, the clip view and the scroll view, without any results.

fittingSize of the document view reports the correct size.

The problem is that the document view, which holds subviews, is not re-sized when the subviews change their size, even if they call invalidateIntrinsicContentSize. The contents of the document view are hence clipped to the original size of the document view as they grow. The document view is created in a nib and set as the scroll view's document view in an awakeFromBib method.

I was hoping that the document view frame would automatically be adjusted when its fittingSize changes, and the scrollbars updated accordingly.

NSPopover does something similar - provided that the subviews of the content controller's view have the constraints set right and various content hugging values are high enough (higher than the hidden popover window's hight constraint priority, for one).

like image 474
Monolo Avatar asked May 29 '12 20:05

Monolo


1 Answers

The problem of course is that when adding the document view, Cocoa will automatically create some hard constraints in the view that the document view is inserted into, i.e., the clip view.

So the answer to my own question is simple, just use:

// Assume self.docView is an IBOutlet populated with
// an NSView subclass
self.docView.translatesAutoresizingMaskIntoConstraints = NO;

before you add the document view to the scroll view:

self.scrollView.documentView = self.docView;

Now, since there are no auto-generated constraints on the layout of the document view in the clip view, you will need to add them explicitly. Otherwise, the doc view's contents will just be rendered at their intrinsic size in the upper left corner of the scroll view.

like image 113
Monolo Avatar answered Sep 20 '22 18:09

Monolo