问题
How to get the Dynamic Height Of UITableView with Constraints. It will increase the UITableView Height depending upon Number of row without adding Scrollbar in table. I'm facing problem in adding the TableView in UIViewController and Height of TableView Should not be Statically fixed.
回答1:
The most concise way I've found to do this is by using the contentSize
of the table view as the intrinsicContentSize
Your tableview needs to be an actual subclass in which you override both contentSize
and intrinsicContentSize
like this (Note that the contentSize just has an added observer, its not really overridden):
override var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
return contentSize
}
Now, when you set up your table view, make sure you set its content compression resistance and content hugging priority to required, then it should size itself within your view automatically based on its intrinsic content size.
来源:https://stackoverflow.com/questions/44852663/dynamic-height-of-uitableview