Created
May 3, 2020 05:36
-
-
Save keyle/e5cc5f7cd0860cf02136032263ea529a to your computer and use it in GitHub Desktop.
NSImageView with Fill Aspect Ratio for Swift 4/5+
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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