ios - 如何创建一个可以自动调整高度的表格

标签 ios uitableview autolayout nslayoutconstraint

我有一个 tableview,它的高度是屏幕的 70%,有时我没有足够的行来填满所有 70%,那么在那种情况下,会有一些带有空白行的空白空间,看起来很尴尬。所以如果高度自动调整到 tablerows 内容,那就太好了,但我找不到与此相关的任何内容。

那么怎么做呢?

最佳答案

你可以像下面那样做

  • 获取UITableView的高度限制的IBOutlet

你会根据你的数据/行找到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()
} 

关于ios - 如何创建一个可以自动调整高度的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46484162/

相关文章:

ios - 如何从另一种方法重新加载 Tableview 数据

ios - 如何在swift中的动态tableview中创建动态tableview?

ios - UITableView, UITableViewCell 内容在设备旋转时消失

iOS 使两个 View 覆盖全 View

ios - 如何从嵌入在导航 Controller 中的简单 View Controller 调用选项卡栏 View Controller ?

objective-c - 网络服务器 NSURL NSURLConnection

ios - 通过 UISlider 更改 UITextView 的字体大小

ios - TableView 的 UIView 无法滚动或点击

ios - 具有自动布局和最大尺寸保持纵横比的 UIImageView

xcode - 4 iPhone 5 和 iphone 6 上具有相等间距的 UIlabel