import Foundation
final class SettingsCollectionViewCell: UICollectionViewListCell, Reusable {
// MARK: - Properties
var rowType: SettingsCollectionViewDataSource.Row? {
didSet {
updateContent()
}
}
private let imageConfig = UIImage.SymbolConfiguration(textStyle: .body, scale: .small)
// MARK: - Subviews
private lazy var containerStack: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [imageStack, textLabel])
stackView.axis = .horizontal
stackView.alignment = .leading
stackView.spacing = 16
return stackView
}()
private lazy var imageStack: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [UIView(), imageContainerView, UIView()])
stackView.axis = .vertical
stackView.distribution = .fill
return stackView
}()
private lazy var imageContainerView: UIView = {
let view = UIView()
view.clipsToBounds = true
view.layer.masksToBounds = true
view.layer.cornerRadius = 5
view.backgroundColor = .gray
view.addSubview(imageView)
NSLayoutConstraint.activate([
view.heightAnchor.constraint(equalTo: view.widthAnchor),
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
if traitCollection.preferredContentSizeCategory.isAccessibilityCategory {
view.isHidden = true
}
return view
}()
private lazy var imageViewWidthConstraint: NSLayoutConstraint = imageContainerView.widthAnchor
.constraint(equalToConstant: UIFontMetrics(forTextStyle: .body).scaledValue(for: 28))
private lazy var imageView: UIImageView = {
let image = UIImage(systemName: "popcorn.fill")?.withConfiguration(imageConfig)
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
imageView.tintColor = .white
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
private lazy var textLabel: UILabel = {
let label = UILabel()
label.adjustsFontForContentSizeCategory = true
label.font = .preferredFont(forTextStyle: .body)
label.numberOfLines = 0
return label
}()
// MARK: - Lifecycle
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
imageStack.isHidden = traitCollection.preferredContentSizeCategory.isAccessibilityCategory
imageViewWidthConstraint.isActive = false
imageViewWidthConstraint = imageContainerView.widthAnchor
.constraint(equalToConstant: UIFontMetrics(forTextStyle: .body).scaledValue(for: 28))
imageViewWidthConstraint.isActive = true
}
private func configure() {
accessories = [.disclosureIndicator()]
containerStack.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(containerStack)
NSLayoutConstraint.activate([
imageContainerView.centerYAnchor.constraint(equalTo: textLabel.centerYAnchor),
containerStack.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
containerStack.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
containerStack.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor),
containerStack.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
separatorLayoutGuide.leadingAnchor.constraint(equalTo: textLabel.leadingAnchor),
imageViewWidthConstraint,
])
}
private func updateContent() {
textLabel.text = rowType?.title
imageView.image = rowType?.icon?.withConfiguration(imageConfig)
}
}
¤ Dauer der Verarbeitung: 0.4 Sekunden
¤
*© Formatika GbR, Deutschland