我有一个 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/