0

I have a custom NSView subclass, and I put it in a NSScrollView, and the basics are working fine. It always takes up the full width of its available space, so its constraints take up the whole width of the scroll view. How much vertical space it takes up depends on how much data it has to display, so it can scroll vertically, only. That's all great.

The catch is that if my custom view needs less space than the NSScrollView that it's in, it doesn't expand to fill the visible area. In cases where there's less data than fits in the visible area, I want it to expand downwards -- so that space is available as a drag-and-drop target, among other things.

  • I've tried changing its "hugging priority". I've tried adding a constraint to keep the bottom below the NSClipView's bottom. I can't come up with any constraint-based solution to fix this (though I haven't ruled out the possibility, either).

  • I've tried catching the notifications when the NSScrollView changes size, and adjusting the custom view's frame if it's too small, but (presumably because I can only change its current frame, while the layout system does all layout later) I can't seem to make this work, either.

Is there a trick for adding a view to an NSScrollView such that it expands to the bottom of the visible area, whenever it would otherwise be too short? It seems like it should be so simple, and I've done it before in cases where I just call -setFrame on everything manually, but once you move to the autolayout world, that approach stops working.

  • have you tried adding an height constraint "yourView.height >= parentView.height" where parentView is the clipview? I had a similar problem as yours and the following worked for me: myView.width >= clipview.width, myview.height >= clipview.height; and to get rid of IB errors: myview.centerXY = clipview.centerXY (remove at buildtime), setting a placeholder for intrinsicSize of myview – Laszlo Korte Aug 28 '15 at 16:42

1 Answers1

0

Without knowing what your constraints and content hugging and content compression actually look like... Content hugging with at least one edge having >= constraint to superview might do it but you might need to adjust priorities. You might also need to make sure you've implemented intrinsicContentSize in your custom view class this tells the content hugging and compression what they need to knowfirst.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
  • Assume constraints are all the default values, e.g., new Xcode Cocoa app, put an NSScrollView in the window, make a new NSView subclass, set the document view to that class, and run. The document view doesn't expand to the scroll view (as evidenced by logging it, or having it draw as `NSRectFill([self bounds])`), even if it has an `intrinsicContentSize`, and even with vertical hugging priority 1. – Lonelyisland Jan 24 '14 at 01:00