您需要在UIViewController中覆盖updateViewConstraints()
方法,并将高度约束的常量设置为tableView.contentSize.height:
override func updateViewConstraints() {
tableHeightConstraint.constant = tableView.contentSize.height
super.updateViewConstraints()
}
那么您必须确保Label2
具有与表视图底部的上约束greaterThanOrEqual
,并且还必须将表视图的高度约束优先级从Required
更改为High
,以避免当表视图的contentHeight
大于可用高度时出现冲突的约束。
updateViewConstraints
方法的末尾调用super.updateViewConstraints()
方法是否有帮助?因为根据文档,这是覆盖该方法的正确方式。 - Iulian Onofrei还有另一种方法可以做到这一点。
您可以在表视图contentSize
变量上添加一个观察者,并在表视图内容发生更改时自我调整大小。
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableHeightConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
tableView.layer.removeAllAnimations()
tableHeightConstraint.constant = tableView.contentSize.height
UIView.animate(withDuration: 0.5) {
self.updateViewConstraints()
}
}
deinit
中使用 removeObserver
来移除观察者。 - lawicko
- 为 tableViewHeight 创建一个 outlet。
@IBOutlet weak var tableViewHeight: NSLayoutConstraint!
var tableViewHeight: CGFloat = 0 // Dynamically calcualted height of TableView.
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.estimatedRowHeight
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print(cell.frame.size.height, self.tableViewHeight)
self.tableViewHeight += cell.frame.size.height
tableViewBillsHeight.constant = self.tableViewHeight
}
解释:
在创建 TableView 单元格后,我们获取即将显示的单元格的框架高度,并将单元格的高度添加到主 TableView 中。
viewDidLayoutSubviews()
中使用此代码。DispatchQueue.main.async {
var frame = self.TblView.frame
frame.size.height = self.TblView.contentSize.height
self.TblView.frame = frame
}
2) 基于表视图高度约束更新表视图高度。当从服务器获取响应并重新加载表视图时,请编写此代码。
DispatchQueue.main.async {
self.TblViewHeightConstraint.constant = CGFloat((self.array.count) * 30)//Here 30 is my cell height
self.TblView.reloadData()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let contentheight = tableView.contentSize.height
self.tableHeightConstraint.constant = contentheight
}
将 @nova 的答案扩展到 Swift 4
tableViewSizeObservation = tableView.observe(\.contentSize) { [weak self] (tableView, _) in
tableView.layer.removeAllAnimations()
self?.tableViewHeightContraint.constant = tableView.contentSize.height
UIView.animate(withDuration: 0.5) {
self?.view.updateConstraints()
self?.view.layoutIfNeeded()
}
}
deinit {
tableViewSizeObservation?.invalidate()
}
override func updateViewConstraints() {
tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true
super.updateViewConstraints()
}
updateViewConstraints
时,您都会创建一个新的约束。因此,您正在累积多个高度约束,因此如果tableView.contentSize更改,则会有具有冲突值的高度约束。因此,值得存储高度约束并始终更新该约束(可以在viewDidLoad中创建它)。此外,即使大小永远不会更改,自动布局也必须处理额外的约束。 - androidguy要使TableView的高度根据内容大小自动调整,请在ViewController类中使用以下代码。
//constraintTableViewHeight :- tableview height constraint
override func viewWillLayoutSubviews() {
super.updateViewConstraints()
self.constraintTableViewHeight.constant = self.tableView.contentSize.height
}
并且添加这个
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
self.viewWillLayoutSubviews()
}
这段代码可以根据内容大小自动调整表格视图的高度。
UITableView
的子类,将其自己的intrinsicContentSize.height
设置为其contentSize.height
。请参见https://dev59.com/uGQm5IYBdhLWcg3wyhfk#17335818。 - rob mayoff