macOS API Experts to the Rescue?

I spent way too long with this now: I need a magnifying ScrollView for a macOS only project, and so I decided to create a custom one with declares. Which basically works nicely, at least until I try to add a Canvas to its documentView and change the magnification factor.
Apparently, macOS wants to handle the layout via NSLayoutConstraints which I included, but the C and Swift examples I tried to translate still don’t give me something usable. Did anyone tackle this control fully? And/or would someone be willing to give some advice?

I did this several years ago; but gave up because of too many conflicts and decided to roll my own magnification solution in the canvas that I was using on the scroll view… But then I ran into a problem whereby the canvas became too big for the scroll view and it would just crap out a lot.

In the end I relented and went back to scrollbars and a canvas, and just captured the magnify gesture.

I personally have a spent a lot of time with the scrollview and Xojo, time that looking back on the matter was wasted. I’d say it’s fine for simple things, but gets very messy for complicated tasks, I am guessing this because Xojo doesn’t officially support the scrollView.

@Ulrich Bogun — Trying to mix Xojo objects and NSObjects is really asking for troubles, IMHO, especially in your case because of all the messages NVIew, NSClipView and NSScrollView exchange.

If you don’t need any or little interaction with the NSView, you may use a Canvas then draw its content into your NSView (see NSView’s bitmapImageRepForCachingDisplayInRect: and cacheDisplayInRect:toBitmapImageRep:)

Thanks a lot! While it feels good to know it’s apparently not my stupidity, being unable to implement native magnification is of course not that satisfying.
From the StackOverflow and similar hints I found, I am uncertain in the weird behavior is really a problem of Xojo. You can easily find the same (controls changing their bounds after a magnification, FocusRings drifting away from the control etc.) in Xcode too. It feels like an IKEA furniture with one page of the assembly comic strip missing.

Anyway, overriding the magnification events or drawing the image from a hidden Canvas sound like ideas worth exploring. I was even considering building a ScrollView from a NIB file. OK, stay tuned for my surrender note …

@Ulrich Bogun — It all really depends on the level of interaction you want. For displaying/zooming a still image, it should be pretty straightforward in Xojo. If you need some simple interaction, like clicking/dragging limited to the displayed area, it should be manageable. However, if you need a full-fledged image-editor and/or complex interactions, that will be tricky.

If you need image editing capabilities, you should have a look at the built-in ImageKit/IKImageView (I think that class is used in

Thanks again! That sounds pretty much like the basic control I was looking for. I receive CGImage data and want to display it as fast as possible (in other words, without having to create a NSImage from it for display in an NSImageView or Canvas paint).

@Ulrich Bogun — Well, creating an NSImage is quite straightforward from a CGImage and it can help you a lot (initWithCGImage:size: passing NSZeroSize).

To display an NSImage as a “backdrop” in an NSView, you can use (pseudo-code):

view.layer.contents = image     (as NSImage or CGImage)

but I am not sure about changing the scale

Well that is something I did not try yet. I am using layer backed views anyway – so there could indeed be the chance of simply using the document view’s layer. Changing the scale could be done with a CGBitmapContext or a CIFIlter. Thanks a lot again!