How can I vertically centre the NSImageCell inside a NSImageView

433 views Asked by At

I have a NSImageView which dynamically displays an image. I'd like the image to be vertically centred in the image view's bounds. How can I achieve this? I think the code I've written makes sense, but NSImageAlignment doesn't seem to have any effect.

    imageView            = NSImageView()
    let image            = NSImage.init(byReferencingFile: "testimage.jpg")
    imageView!.image           = image
    imageView!.frame  = NSMakeRect(frameRect.origin.x, frameRect.origin.y, 180, 300)
    imageView!.imageFrameStyle = NSImageFrameStyle.Photo
    imageView!.imageScaling    = NSImageScaling.ScaleProportionallyUpOrDown
    imageView!.imageAlignment  = NSImageAlignment.AlignCenter

Trying to figure this out I've learnt that NSImageViews don't have subviews, but NSImageCells inside of them, throwing me off from how UIImageViews work. I don't want to centre the NSImageView, but the NSCell inside the NSImageView.

I've tried subclassing NSImageView as detailed here but when I do this I lose scaling.

How can I vertically centre the NSImageCell inside a NSImageView?

pixels

0

There are 0 answers