Skip to content

Instantly share code, notes, and snippets.

@keyle
Created May 3, 2020 05:36
Show Gist options
  • Select an option

  • Save keyle/e5cc5f7cd0860cf02136032263ea529a to your computer and use it in GitHub Desktop.

Select an option

Save keyle/e5cc5f7cd0860cf02136032263ea529a to your computer and use it in GitHub Desktop.
NSImageView with Fill Aspect Ratio for Swift 4/5+
//
// NSImageViewFill.swift
// NSImageView with Fill Aspect Ratio
// Swift 4.0+
//
// Written by (source) https://stackoverflow.com/a/46262418/200041
// I take no credit in this but it's too awesome not to gist.
//
import Cocoa
open class NSImageViewFill : NSImageView {
open override var image: NSImage? {
set {
self.layer = CALayer()
self.layer?.contentsGravity = CALayerContentsGravity.resizeAspectFill
self.layer?.contents = newValue
self.wantsLayer = true
super.image = newValue
}
get {
return super.image
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment