Skip to content

Instantly share code, notes, and snippets.

@userow
Created May 13, 2023 10:11
Show Gist options
  • Save userow/cb4fbae0d01a05b30f0a812302839400 to your computer and use it in GitHub Desktop.
Save userow/cb4fbae0d01a05b30f0a812302839400 to your computer and use it in GitHub Desktop.
TableViewCell of test task with Stack issues
class CountryCellView: UITableViewCell {
// MARK: - Declarations. Private
// MARK: UI elements
private let _labelName = UILabel()
private let _labelFlag = UILabel()
private let _labelCode = UILabel()
private let _stackV = UIStackView()
private let _stackH = UIStackView()
// MARK: state variables
private var _countryInfo: CountryInfo? = nil
private var _state = CountryCellState()
// MARK: - Initialize
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubviews()
createConstraints()
}
/*
1) structure
contentView
stackH
labelFlag
stackV
labelName
labelCode
*/
func addSubviews() {
_labelName.translatesAutoresizingMaskIntoConstraints = false
_labelName.numberOfLines = 0
_labelName.textColor = .label
_labelName.textAlignment = .justified
_labelName.setContentHuggingPriority(UILayoutPriority.defaultLow, for: NSLayoutConstraint.Axis.horizontal)
_labelName.backgroundColor = .red
_labelCode.translatesAutoresizingMaskIntoConstraints = false
_labelCode.textColor = .secondaryLabel
_labelCode.textAlignment = .justified
_labelCode.backgroundColor = .green
_stackV.translatesAutoresizingMaskIntoConstraints = false;
_stackV.axis = NSLayoutConstraint.Axis.vertical
_stackV.distribution = UIStackView.Distribution.fill
_stackV.alignment = UIStackView.Alignment.leading
_stackV.contentMode = .scaleToFill
_stackV.spacing = 8
_stackV.addArrangedSubview(_labelName)
_stackV.addArrangedSubview(_labelCode)
_stackV.backgroundColor = .blue
_labelFlag.translatesAutoresizingMaskIntoConstraints = false;
_labelFlag.textColor = .label
_labelFlag.textAlignment = .center
_labelFlag.backgroundColor = .yellow
_stackH.translatesAutoresizingMaskIntoConstraints = false;
_stackH.axis = NSLayoutConstraint.Axis.horizontal
_stackH.distribution = UIStackView.Distribution.equalSpacing
_stackH.alignment = UIStackView.Alignment.center
_stackH.contentMode = .scaleToFill
_stackH.spacing = 8
_stackH.backgroundColor = .orange
_stackH.addArrangedSubview(_labelFlag)
_stackH.addArrangedSubview(_stackV)
self.contentView.addSubview(_stackH)
_labelName.text = "Barbados" "🇧🇧")
_labelCode.text = "BB"
_labelFlag.text = "🇧🇧"
}
func createConstraints() {
NSLayoutConstraint.activate([
_stackH.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
_stackH.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
_stackH.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
_stackH.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
// _labelFlag.widthAnchor.constraint(equalToConstant: 44),
// _labelFlag.heightAnchor.constraint(equalToConstant: 44),
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment