我已经尝试解决这个问题将近一天了。所以点应该与它旁边的 UILabel 对齐。问题是,当填充正确的内容时,第二个标签有时会变成两行。该点必须与 UILabel 的第一行对齐,无论它有 1 行还是两行。
我尝试在更新单元格时在运行时调用 sizeToFit()
。好像没什么效果我读了Vertically align text to top within a UILabel线程。
我模拟如果 sizeToFit()
(Editor -> Size to fit content) 被成功调用,行的高度不会缩小。
每一行由 UIView 组成,在这个 View 中有点和 UILabel。点也是 UIView。具有单行 UILabel 的行的高度常量为 20。可能有 2 行 UILabel 的行我设置为 <= 26。
点必须与 UILabel 的第一行对齐,无论它有 1 行还是两行。这在较小的屏幕上效果很好。
有人可以帮忙吗?
最佳答案
<强>1。 UIViewController 层次结构
<强>2。约束
查看主要内容 BG
- top、bottom、leading、trailing = 0 wrt superview。
lbl1
- top = 10 , trailing = 10 wrt superview
点 1
- leading = 10 top = lbl1.top,到 lbl1 的水平空间 = 10,高度 = 10 宽度 = 10
lbl2
- Verticle space wrt
lbl1
= 10 , leading & trailing = lbl1.leading & lbl1.leading
- Verticle space wrt
dot2
- leading = dot1.leading , top = lbl2.top , height = 10 width = 10
lbl3
- 垂直空间 wrt
lbl2
= 10,leading & trailing = lbl2.leading & lbl2.leading and bottom = 10 wrt superview
- 垂直空间 wrt
点 1
- leading = dot2.leading , top = lbl3.top , height = 10 width = 10
不要忘记保持每个标签的行数 = 0。并使用 VC 绑定(bind) TableView 委托(delegate)和数据源。
<强>3。现在你的 Storyboard设计看起来像这样
<强>4。现在在 VC 中
虚拟数组
var arrList : [[String : String]] = [["lbl1": "Do any additional setup after loading the view, typically from a nib.",
"lbl2": "Do any additional.",
"lbl3": "that can be recreated"],
["lbl1": "Do any additional .",
"lbl2": "Do any additional setup after loading the view, typically from a nib.",
"lbl3": "that can be recreated"],
["lbl1": "Do any additional . Do any additional setup after loading the view, typically from a nib.",
"lbl2": "Do any additional setup after loading the view, typically from a nib.",
"lbl3": "that can be recreated Do any additional setup after loading the view, typically from a nib. Do any additional setup after loading the view, typically from a nib."]]
TableView 委托(delegate)和数据源
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 110
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return arrList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "TblCell") as! TblCell
let dict = arrList[indexPath.row]
cell.lbl1.text = dict["lbl1"]
cell.lbl2.text = dict["lbl2"]
cell.lbl3.text = dict["lbl3"]
return cell
}
<强>5。最终输出
Edit
如果您使用常量 = 5 wr 和它们的 respec 标签设置点的顶部,则输出在下方。
关于ios - UITableViewCell `sizeToFit` 内的 UILabel 无法在使用 AutoLayout 的单元格内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47919986/