3

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

Community
  • 1
  • 1
glenstorey
  • 5,134
  • 5
  • 39
  • 71
  • I think that you want to add constraints. Check out http://stackoverflow.com/questions/26180822/swift-adding-constraints-programmatically – Kendall Crouch Feb 09 '16 at 17:14
  • try setting all the margins to be auto-resizable if you want the image to be centered like in here: http://stackoverflow.com/questions/4681176/how-to-align-a-subview-to-the-center-of-a-parent-nsview – Guy Daher Feb 09 '16 at 17:31
  • k, I've done a bit of learning on how NSImageViews work, and updated my question to make more sense. I don't think constraints or margins are what I'm asking here; I just want the image cell centred in the nsimageview. – glenstorey Feb 10 '16 at 17:44

0 Answers0