-
-
Save thanhit93/09dfd8cf629e25c9922a4919b2f94658 to your computer and use it in GitHub Desktop.
SWIFT - Observe contentSize fro the tableView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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