Created
December 20, 2021 15:50
-
-
Save thomsmed/eb39ca8a12d4ae22f23887b704078890 to your computer and use it in GitHub Desktop.
TableViewCell.swift - From "Transition images to full screen animated" @ medium.com
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
| // | |
| // TableViewCell.swift | |
| // | |
| import UIKit | |
| import TinyConstraints | |
| class TableViewCell: UITableViewCell { | |
| private let octocatNameLabel: UILabel = { | |
| let label = UILabel() | |
| label.font = .systemFont(ofSize: 20) | |
| label.textAlignment = .center | |
| label.setHugging(.defaultHigh, for: .vertical) | |
| label.setCompressionResistance(.defaultHigh, for: .vertical) | |
| return label | |
| }() | |
| private let octocatImageView: UIImageView = { | |
| let imageView = UIImageView() | |
| imageView.contentMode = .scaleAspectFit | |
| imageView.setHugging(.defaultHigh, for: .horizontal) | |
| imageView.setCompressionResistance(.defaultHigh, for: .horizontal) | |
| return imageView | |
| }() | |
| private lazy var imageViewWidthConstraint = octocatImageView.width(.zero) | |
| private let imageHeight: CGFloat = 200 | |
| override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | |
| super.init(style: style, reuseIdentifier: reuseIdentifier) | |
| configureUI() | |
| } | |
| required init?(coder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| private func configureUI() { | |
| selectionStyle = .none | |
| contentView.addSubview(octocatNameLabel) | |
| contentView.addSubview(octocatImageView) | |
| octocatNameLabel.topToSuperview(offset: 16) | |
| octocatNameLabel.leadingToSuperview(offset: 16) | |
| octocatNameLabel.trailingToSuperview(offset: 16) | |
| octocatImageView.topToBottom(of: octocatNameLabel, offset: 16) | |
| octocatImageView.height(imageHeight) | |
| octocatImageView.centerXToSuperview() | |
| octocatImageView.bottomToSuperview(offset: -16) | |
| } | |
| func setup(with octocat: Octocat, and tag: Int) { | |
| octocatNameLabel.text = octocat.name | |
| octocatImageView.image = UIImage(contentsOfFile: octocat.imagePath) | |
| octocatImageView.tag = tag | |
| // Constraint UIImageView to fit the aspect ratio of the containing image | |
| let aspectRatio = octocatImageView.intrinsicContentSize.width / octocatImageView.intrinsicContentSize.height | |
| imageViewWidthConstraint.constant = imageHeight * aspectRatio | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment