Re: Centering image view in scroll view


Graham Cox
 

You need to subclass NSClipView and implement the behaviour there. It’s annoying that there’s no built-in solution since it’s an ‘obvious’ requirement to me.

The better news is that the needed behaviour isn’t hard to achieve - this stack overflow answer gives a good solution: https://stackoverflow.com/questions/22072105/how-do-you-get-nsscrollview-to-center-the-document-view-in-10-9-and-later


—Graham

On 17 Nov 2017, at 3:04 am, Steve Mills <sjmills@mac.com> wrote:

Ack. I'm having a hard time figuring out how to make an NSImageView in an NSScrollView act like it does in Preview.app, where it will stay centered within the window when any axis is smaller than the window, but still be zoomable by the user. What magic combination of settings, methods, and/or constraints do I need?

I have an NSViewController, if I need to override some methods there. Its view contains the scroll view. I initially want the image to fit within the window, so I calculate and set the scroll view's magnification in viewWillAppear.

Join cocoa@apple-dev.groups.io to automatically receive all group messages.