I'm trying to find the best way to use Lion's autolayout to make this simple window containing a scrollable view with a centered image and a border around it. See my awesome ascii art:
+==============+
| ||
| XXXXXX ||
| XXXXXX ||
| XXXXXX ||
| |
+--------------+
The NSScrollView's document view is a flipped NSView subclass that draws a background pattern, and inside that I add an image that the user chooses (ie. indeterminate size). When adding the image view, I give it constraints to center it in the document view. The document view itself is given constraints saying it wants to be slightly larger than the loaded image. What I'm wanting is the obvious behavior: on loading an image larger than the scollview, the scrollers become active, on resizing the window the scrollers activate / deactivate normally.
However, when loading a large image I'm seeing is the document view expanding all right, but the scroll view & window expand with it! After that point the window doesn't permitting resizing any smaller. Obviously I'm not doing it right, but I can't see why.
I tried changing the priority of the constraints of the document view, anything above NSLayoutPriorityWindowSizeStayPut and the window expands as above (makes sense I guess), but anything lower and the document view never resizes, resizing the window smaller works but the scrollers aren't enabled.
Does anyone know if there are other priorities I need to set elsewhere, or additional constraints I need, or a different approach altogether .. should I instead be setting the document view's "intrinsic size"? Also pointers to any autolayout open source / sample code I haven't seen yet would be cool.
NSScrollView
, the view hierarchy containing theNSImageView
being created at runtime so that thetranslates..
property can be set to NO. I can't remember what the contraints were, but I don't think they were tricky. Probably aNSView
superview to theNSImageView
having width/height constraints set with>=
relation to the image size (known at the time the constraint made at runtime), then ones for centering the NSImageView in its superview. – Pierre Houston