ios - UITableViewCell `sizeToFit` 内的 UILabel 无法在使用 AutoLayout 的单元格内工作

标签 ios swift autolayout uilabel

我已经尝试解决这个问题将近一天了。所以点应该与它旁边的 UILabel 对齐。问题是,当填充正确的内容时,第二个标签有时会变成两行。该点必须与 UILabel 的第一行对齐,无论它有 1 行还是两行。

enter image description here

我尝试在更新单元格时在运行时调用 sizeToFit()。好像没什么效果我读了Vertically align text to top within a UILabel线程。

我模拟如果 sizeToFit() (Editor -> Size to fit content) 被成功调用,行的高度不会缩小。

enter image description here

每一行由 UIView 组成,在这个 View 中有点和 UILabel。点也是 UIView。具有单行 UILabel 的行的高度常量为 20。可能有 2 行 UILabel 的行我设置为 <= 26。

点必须与 UILabel 的第一行对齐,无论它有 1 行还是两行。这在较小的屏幕上效果很好。

enter image description here

有人可以帮忙吗?

最佳答案

<强>1。 UIViewController 层次结构

enter image description here

<强>2。约束

  1. 查看主要内容 BG

    • top、bottom、leading、trailing = 0 wrt superview。
  2. lbl1

    • top = 10 , trailing = 10 wrt superview
  3. 点 1

    • leading = 10 top = lbl1.top,到 lbl1 的水平空间 = 10,高度 = 10 宽度 = 10
  4. lbl2

    • Verticle space wrt lbl1 = 10 , leading & trailing = lbl1.leading & lbl1.leading
  5. dot2

    • leading = dot1.leading , top = lbl2.top , height = 10 width = 10
  6. lbl3

    • 垂直空间 wrt lbl2 = 10,leading & trailing = lbl2.leading & lbl2.leading and bottom = 10 wrt superview
  7. 点 1

    • leading = dot2.leading , top = lbl3.top , height = 10 width = 10

不要忘记保持每个标签的行数 = 0。并使用 VC 绑定(bind) TableView 委托(delegate)和数据源。

<强>3。现在你的 Storyboard设计看起来像这样

enter image description here

<强>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。最终输出

enter image description here

Edit

如果您使用常量 = 5 wr 和它们的 respec 标签设置点的顶部,则输出在下方。

enter image description here

关于ios - UITableViewCell `sizeToFit` 内的 UILabel 无法在使用 AutoLayout 的单元格内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47919986/

相关文章:

ios - 来自 UIActivityItemSource 的多个数据

xcode - 如何使用Xcode的自动布局调整 View 大小

ios - Xcode 中的本地化按钮有什么作用?

swift - 如何按字母顺序对注释数组进行排序

ios - 在 Swift 中创建灵活的 API 类

Swift 在更改 viewcontroller 后选择一行

ios - UITableView 单元格内容未正确显示

swift - 单击按钮后标签会在左上角上升

ios - navigationBar出现在statusBar下面

ios - 这些是我的泄密吗?