Search code examples
cocoansoutlineviewnsscrollview

NSOutlineView doesn't adjust NSScrollView document view size


I have an NSOutlineView that I have programmatically added into an NSScrollView:

outlineView = ECOutlineView()
outlineView.translatesAutoresizingMaskIntoConstraints = false
scrollView.documentView = outlineView

The scrollview is constrained to the window it is in. Everything behaves as expected except for the fact that the scrollView doesn't react to changes in the outlineView length.

For example, if a row is expanded, lengthening the outline view, the scrollview does not adjust to allow scrolling to the new content.

Some scroll synchronisation does work. If I scroll down to the end of the outliner and start deleting rows, the scroll position is updated so that the new bottom row is aligned with the bottom of the scroll view, however the scroll scope is still incorrect.

If I resize the window slightly, the scrollView will update and give the correct scroll scope.

So, it looks as though constraints are correct, I just need to force an update - but shouldn't this be happening automatically?


Solution

  • You shouldn't set translatesAutoresizingMaskIntoConstraints to false for a scroll view's document view. In general, a container view is responsible for controlling that property for the views it's managing, because only it knows how it's managing the position. And, you should leave it at the default in case you're working with a container view that doesn't know about auto layout.

    By "container view", I mean a class which is specifically designed to manage other views' placement. I don't just mean any view which may have subviews.

    For the enclosing scroll view, if you're just putting that in a plain view or a view class of your own, and you're using auto layout to control its placement, then you would still need to set translatesAutoresizingMaskIntoConstraints to false.