I'm trying to get the height of all of a table's contents so I can update the container it is in. This will allow the container and other views in the scroll view to scroll together.
Asked
Active
Viewed 3.0k times
20
-
Possible duplicate of [Set UITableView's height to the height of its content with Auto Layout](http://stackoverflow.com/questions/33268463/set-uitableviews-height-to-the-height-of-its-content-with-auto-layout) – Hamed Nova Apr 20 '17 at 12:52
-
see [this](http://stackoverflow.com/a/43520073/1598912) i answered the same question.good luck – Hamed Nova Apr 20 '17 at 12:53
3 Answers
51
Swift:
var tableViewHeight: CGFloat {
tableView.layoutIfNeeded()
return tableView.contentSize.height
}
Objective-C
- (CGFloat)tableViewHeight {
[tableView layoutIfNeeded];
return [tableView contentSize].height;
}

CodeBender
- 35,668
- 12
- 125
- 132
-
4I think setting height based on tableview content height is not good way as its depend on when you are getting table content height...If you call this method right after reloading table then it may be possible that you might get wrong content size – vivek bhoraniya May 04 '17 at 11:40
31
Swift 3
override func viewDidLoad() {
super.viewDidLoad()
myTbleView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
override func viewWillDisappear(_ animated: Bool) {
myTbleView.removeObserver(self, forKeyPath: "contentSize")
super.viewWillDisappear(true)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if(keyPath == "contentSize"){
if let newvalue = change?[.newKey]
{
let newsize = newvalue as! CGSize
tableViewHeightConstraint.constant = newsize.height
}
}
}
Hope this will help you.

ram880
- 1,288
- 12
- 22
-
2This code is working like a charm, but I have a problem I have two tableview in a same view controller, then this code set the same height for both the tableview's. So is there any other way to get the height of tableview separately. – Atpl Mohali Aug 23 '18 at 07:49
-
1if (object as! UITableView) == firstTableView {} else if (object as! UITableView) == secondTableView {} – Aman Gupta Oct 10 '18 at 13:05
-
1Thanks! I think it would be best to add the observer in viewWillAppear, if you modally present another view controller or have a tab bar etc, you will remove the observer and not add it on return. The functionality will no longer be there and the app will crash when trying to remove an observer that is not added. – Trevor Oct 18 '18 at 07:11
-
Its working but i just notify that your observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call Observer call that much time execute. – Himanshu Moradiya Oct 19 '18 at 04:30
-
I think creating a custom Notification that fires only when your data is loaded will solve the problem, the method above works but it will call observer many times... it might cause your app to stutter. – Bruno Henrique May 12 '20 at 01:25
-
1
3
var obs: NSKeyValueObservation?
obs = tableView.observe(\.contentSize, options: .new) { (_, change) in
guard let height = change.newValue?.height else { return }
self.constantHeight.constant = height
}

Ahmed Safadi
- 4,402
- 37
- 33