Created
January 30, 2017 17:38
-
-
Save efremidze/1cabc32875fb44b3773047e698ad7d5d to your computer and use it in GitHub Desktop.
This file contains 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
import UIKit | |
import AVFoundation | |
class ResizingImageView: UIImageView { | |
private var heightConstraint: NSLayoutConstraint? | |
override var image: UIImage? { | |
didSet { | |
_ = heightConstraint.map { NSLayoutConstraint.deactivate([$0]) } | |
var boundingRect = self.bounds | |
boundingRect.size.height = .greatestFiniteMagnitude | |
let rect = AVMakeRect(aspectRatio: image?.size ?? CGSize(), insideRect: boundingRect) | |
heightConstraint = self.heightAnchor.constraint(equalToConstant: rect.height) | |
_ = heightConstraint.map { NSLayoutConstraint.activate([$0]) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment