Skip to content

Instantly share code, notes, and snippets.

@thanhit93
Forked from cscouto/ViewController.Swift
Created August 28, 2020 15:44
Show Gist options
  • Save thanhit93/09dfd8cf629e25c9922a4919b2f94658 to your computer and use it in GitHub Desktop.
Save thanhit93/09dfd8cf629e25c9922a4919b2f94658 to your computer and use it in GitHub Desktop.
SWIFT - Observe contentSize fro the tableView
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var heightTableView: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
tableView.removeObserver(self, forKeyPath: "contentSize")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let obj = object as? UITableView {
if obj == self.tableView && keyPath == "contentSize" {
heightTableView.constant = tableView.contentSize.height
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment