【问题标题】:How to create a table whose height can be autoadjusted如何创建可以自动调整高度的表格
【发布时间】:2017-09-29 07:47:55
【问题描述】:

我有一个表格视图,其高度是屏幕的 70%自动调整到 tablerows 内容然后它会很棒但我找不到任何相关的东西。

那该怎么做呢?

【问题讨论】:

标签: ios uitableview autolayout nslayoutconstraint


【解决方案1】:

你可以像下面那样做

  • IBOutlet 的高度限制为UITableView

你会根据你的数据/行找到tableView的高度

tableView.contentSize.height

现在您需要根据以下条件更改高度限制

if  tableView.contentSize.height < UIScreen.main.bounds.size.height { // Instead of UIScreen.main.bounds.size.height, change as per your requirment

  yourTblHeightConstraints.constant = tableView.contentSize.height
  self.view.layoutIfNeeded()
}
else {
  yourTblHeightConstraints.constant = UIScreen.main.bounds.size.height // Instead of UIScreen.main.bounds.size.height, change here as you need
  self.view.layoutIfNeeded()
} 

【讨论】:

  • 将 self.view.layoutIfNeeded() 替换为 self.view.setNeedsLayout()
  • 如果您在实际应用程序中编写此代码(使用 layoutIfNeeded),有时会出现异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
相关资源
最近更新 更多