Reputation: 9750
I am asking a question almost exactly the same as Trouble with autolayout on NSScrollView's document view. The solution of this question was to set translatesAutoresizingMaskIntoConstraints
to NO
in -awakeFromNib:
.
However, for me (I am using Xcode 5), this flag is already set to NO
when loading the nib so I seem to be running into a different issue.
I would like to replicated the way Preview.app details with image zooming, just using layout constraints (if possible). When the image size is less than the content view bounds the full image is displayed. However, when the image is zoomed the scroll bars activate to allow navigation. This seems simple enough. Below is the view hierarchy I have constructed. Note that the image view is contained inside a custom NSView (this is because I have been following the previous question approach where the custom view draws a background), this container view is to the NSScrollView's documentView
and than the views where embedded inside the scroll view using the IB's Embed In command.
I have setup the following constraints in IB:
intrinsicContentSize
is set to the size of the image. In IB a placeholder size is used to make the constraints happy.These constraints are not ambiguous, but I don't understand why the image is not centred?
Upvotes: 2
Views: 814
Reputation: 9351
Old question, but here's a modern solution that doesn't require subclassing. There are four views involved here:
So here's the process:
Note that you could replace the NSImageView with more or less anything that supports content alignment and intrinsic content size. Alternatively, use an NSView, centre some other content inside it and pin that content's top and left edges to the parent. This will allow the NSView to infer its minimum size whilst keeping its child central.
Upvotes: 2
Reputation: 17491
This seems like a reasonable result given how you have described the problem. According to above, you have constrained:
The NSScrollView
won't use auto layout to lay out the views within itself. Thus, by setting the image container view to be the size of the image, NSScrollView
will pin that to the lower left (its origin point) when you have content that is too small.
When I've run into this same problem, I have done 2 things, each requiring some work and each reasonably successful, either:
NSScrollView
's content area and then center the NSImageView
inside of that view. This gets the auto-centering inside of the containing view, but requires you to manipulate the containing view's size in order to not have a border when the image is zoomed in.or
NSScrollView
and center the containing view on the window when the containing view is too small to fill the NSScrollView
.Either way, you need to pay attention to when the window's and NSScrollView
's content areas change and when the size of the NSImageView
changes.
Upvotes: 1