【问题标题】:Give bottom constraint to the view in uitableviewcell which has more height为具有更高高度的 uitableviewcell 中的视图提供底部约束
【发布时间】:2018-05-14 13:22:10
【问题描述】:

我有以下 UITableViewCell,其中包含标签和图像视图。 image view 的高度是固定的,但是 label 的高度会随着文字的变化而变化。

我怎样才能给出底部约束,即标签高度很小,那么单元格的高度应该根据imageviewsize,如果标签尺寸更大,它应该根据标签 我希望单元格看起来像图像中的样子

【问题讨论】:

  • 尝试将约束添加到 4 边(尾随、前导、顶部和底部),您不应该给标签和图像视图都提供固定高度约束。
  • 但是 Imageview 对于所有单元格的高度都是固定的
  • 如果您使用固定高度,它可能无法更改单元格大小。
  • @user2185354 你的问题还存在吗?

标签: swift uitableview nslayoutconstraint


【解决方案1】:

首先将您的 TableView rowHeight 赋予 UITableViewAutomaticDimension。它将允许 tableView 单元格的高度根据他的内容增长:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

self.tableView.rowHeight = UITableViewAutomaticDimension

因为很容易先给出名称,所以您的 tableViewCell 中有:- ImageView、messageLabel、profileNameLabel 和 timeLabel。

第一步: 给你的 ImageView 约束 TopLeft 到 superView(Content View) 和 timeLabel 到 Right 到 superView 也对齐 将 Y 居中到您的 ImageView

第二步: 现在在中间给你的 messageLable 约束 Top 到 superView,Left 到 imageView 和 Right 到 superView确保 messageLable 行号为 0。

第三步: 然后将您的 profileNameLable 约束 Top 和 Leading 设置为 messageLable

第四步: 因此,现在要使您的 tableViewCell 高度将根据您的内容(messageLable 可变高度)增长。将您的 profileNameLabel bottom Grater than equal to(>=) 限制为 SuperView(ContentView) 例如 20

还有 ImageView 底部 Grater than equal to(>=) 对 SuperView(ContentView) 的约束,例如 20。所以当 messageLabel 有更高的高度它将推动单元格高度,当它具有正常高度时,单元格将从您的 ImageView 中获取高度。

Grater 然后等于 (>=) 约束(这基本上意味着始终保持 ma​​rgin 例如 20 像素空间,因此它可以超过 20 像素但不能更少那么)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多