【发布时间】:2021-06-04 17:01:41
【问题描述】:
我有一个表格视图,每个单元格都包含一个垂直的 UIStackView, 我想将这个堆栈视图中的所有元素居中,每个元素之间的间距为 8px。这是我想要实现的视觉效果:
但这是我目前所得到的:
还有我试过的代码:
在 UITableView 的委托中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TVCell", for: indexPath) as! TVCell
(0...indexPath.row).forEach { i in
let label = UILabel()
label.text = "Label #\(i)"
cell.stackView.addArrangedSubview(label)
}
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
5
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
180
}
TVCell
class TVCell: UITableViewCell {
@IBOutlet weak var stackView: UIStackView!
}
我应该怎么做才能得到预期的结果?
感谢您的帮助
【问题讨论】:
-
尝试将标签的框架设置为它所占据的整个空间。
标签: ios swift uikit uistackview