ios 使用自动布局将 UITableView 的高度设置为其内容的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33268463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set UITableView's height to the height of its content with Auto Layout
提问by Dumpen
I have a View which has two labels and a Table View inside it. I want label 1 to always stay above my Table View and label 2, to be below the Table View. The problem is that the Table View needs to auto-size meaning either increase in height or decrease.
我有一个视图,里面有两个标签和一个表视图。我希望标签 1 始终保持在我的表视图上方,而标签 2 始终位于表视图下方。问题是表格视图需要自动调整大小,这意味着高度增加或减少。
Right now I have a constraint saying the Table View's height is always equal to 85 and a @IBOutlet
to the height constraint where i'm able to change the constant.
现在我有一个约束,说表视图的高度总是等于 85 和@IBOutlet
高度约束,我可以在其中更改常量。
I'm guessing I need to change the constant to the height of all the cells, but i'm not sure how.
我猜我需要将常量更改为所有单元格的高度,但我不确定如何。
回答by joern
You have to override updateViewConstraints()
in your UIViewController and set the height constraint's constant to tableView.contentSize.height:
您必须updateViewConstraints()
在 UIViewController 中覆盖并将高度约束的常量设置为 tableView.contentSize.height:
override func updateViewConstraints() {
tableHeightConstraint.constant = tableView.contentSize.height
super.updateViewConstraints()
}
Then you have to make sure that Label2
has a top constraint that is greaterThanOrEqual
to the table view's bottom. And you also have to change the table view's height constraint's priority from Required
to High
to avoid conflicting constraints when the table view's contentHeight
is larger than the available height.
然后你必须确保Label2
有一个greaterThanOrEqual
对表视图底部的顶部约束。并且您还必须将 table view 的高度约束的优先级从Required
更改High
为,以避免在 table viewcontentHeight
大于可用高度时发生冲突约束。
回答by Hamed Nova
There is another way to do that.
You can add an observer on table view contentSize
variable, and self size when change in table view content.
还有另一种方法可以做到这一点。您可以在 table viewcontentSize
变量上添加观察者,并在 table view 内容更改时自行调整大小。
@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()
}
}
回答by iOS
In Swift 4.2 and Xcode 10.1
在 Swift 4.2 和 Xcode 10.1 中
Here you have two options to set height for UITableView based on content dynamically.
在这里,您有两个选项可以根据内容动态设置 UITableView 的高度。
1) If you get content size use this code in viewDidLayoutSubviews()
1)如果您获得内容大小,请使用此代码 viewDidLayoutSubviews()
DispatchQueue.main.async {
var frame = self.TblView.frame
frame.size.height = self.TblView.contentSize.height
self.TblView.frame = frame
}
2) Based on table view height constraint update table view height. After got the response from server when reload table view write this code.
2) 基于表视图高度约束更新表视图高度。在重新加载表视图时从服务器获得响应后编写此代码。
DispatchQueue.main.async {
self.TblViewHeightConstraint.constant = CGFloat((self.array.count) * 30)//Here 30 is my cell height
self.TblView.reloadData()
}
回答by Faris Muhammed
For TableView Height adjustment automatically with its content size use this in ViewController class.
对于 TableView 高度自动调整及其内容大小,请在 ViewController 类中使用它。
//constraintTableViewHeight :- tableview height constraint
override func viewWillLayoutSubviews() {
super.updateViewConstraints()
self.constraintTableViewHeight.constant = self.tableView.contentSize.height
}
and also add this
并添加这个
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
self.viewWillLayoutSubviews()
}
This code easily adjust tableview height automatically with its content size.
此代码可以轻松地根据其内容大小自动调整 tableview 高度。
回答by Deepika
Extending @nova answer to swift 4
将@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()
}
回答by jocken
A more modern way is doing this:
一种更现代的方法是这样做:
override func updateViewConstraints() {
tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true
super.updateViewConstraints()
}