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:


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

Ack. I'm having a hard time figuring out how to make an NSImageView in an NSScrollView act like it does in, 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 to automatically receive all group messages.