uitableview - 以编程方式在 UITableView 单元格中显示 UIStackView
问题描述
我想在其中显示带有 3 个标签的水平 UIStackView,但我看不到标签...
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let stackView = UIStackView(arrangedSubviews: [label1,label2,label3])
stackView.distribution = .fill
stackView.axis = .horizontal
stackView.spacing = 8
stackView.alignment = .center
backgroundColor = .black
addSubview(stackView)
stackView.leftAnchor.constraint(equalTo: leftAnchor, constant: 8).isActive = true
stackView.rightAnchor.constraint(equalTo: rightAnchor, constant: 8).isActive = true
stackView.topAnchor.constraint(equalTo: topAnchor, constant: 8).isActive = true
stackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 8).isActive = true
}
我尝试过使用锚点,但也没有成功。怎么做?
解决方案
情侣笔记...
您应该将子视图添加到单元格
contentView
,而不是单元格本身。从您显示的代码中,您没有设置
stackView.translatesAutoresizingMaskIntoConstraints = false
您应该使用
leadingAnchor
andtrailingAnchor
而不是 left/right
这是一个入门单元,可以帮助您上路:
class BogdanCell: UITableViewCell {
let label1: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .yellow
v.text = "Label 1"
v.textAlignment = .center
return v
}()
let label2: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .yellow
v.text = "Label 2"
v.textAlignment = .center
return v
}()
let label3: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .yellow
v.text = "Label 3"
v.textAlignment = .center
return v
}()
let theStackView: UIStackView = {
let v = UIStackView()
v.translatesAutoresizingMaskIntoConstraints = false
v.axis = .horizontal
v.alignment = .center
v.distribution = .fill
v.spacing = 8
return v
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() -> Void {
backgroundColor = .black
theStackView.addArrangedSubview(label1)
theStackView.addArrangedSubview(label2)
theStackView.addArrangedSubview(label3)
contentView.addSubview(theStackView)
NSLayoutConstraint.activate([
theStackView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor, constant: 0.0),
theStackView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: 0.0),
theStackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor, constant: 0.0),
theStackView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor, constant: 0.0),
])
}
}
推荐阅读
- python - 有人可以帮我写日志 - 错误:绑定方法吗?
- email - 邮件中的 Message-ID 有什么用?
- node.js - Angular 项目运行不正常
- django - 如何让我的用户通过我的 django 应用程序从他们的 gmail 帐户发送电子邮件?
- ruby - 如何将 Ruby 测试/单元日志记录设置为 quiet_mode?
- spring-cloud-stream - 有没有办法在春云流中启动和停止所有流?
- powershell - PowerShell:控制模块中的详细输出
- varnish - Varnish:为多个同时用户提供一个带有 1 个后端请求/连接的大文件
- python - PyCharm/Visual Studio中MacOS上的python-nmap---Nmap 需要root权限
- css - 如何在 Safari 中将渐变文本放在 btn 上?