Created
August 6, 2021 05:04
-
-
Save rr-codes/0a0f4632c968b0b3d17ef700ff430e70 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
class FlagListTableViewCell: UITableViewCell, ReusableView { | |
private let flagImageView = UIImageView() | |
private var cellHeightConstraint: NSLayoutConstraint! | |
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
self.cellHeightConstraint = self.flagImageView.heightAnchor.constraint(equalToConstant: 0) | |
self.cellHeightConstraint.isActive = true | |
self.flagImageView.contentMode = .scaleAspectFit | |
self.flagImageView.translatesAutoresizingMaskIntoConstraints = false | |
self.contentView.addSubview(self.flagImageView) | |
self.flagImageView.pinEdges(to: self.contentView) | |
} | |
@available(*, unavailable) | |
required init?(coder _: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func layoutSubviews() { | |
let imageViewWidth = self.contentView.frame.width | |
let imageWidth = CGFloat(self.flagImageView.image!.cgImage!.width) | |
let imageHeight = CGFloat(self.flagImageView.image!.cgImage!.height) | |
let scaledHeight = imageHeight * (imageViewWidth / imageWidth) | |
self.cellHeightConstraint.constant = scaledHeight | |
} | |
func bind(to country: Country) { | |
let image = UIImage(named: country.flagImageName)! | |
self.flagImageView.image = image | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment