ios - Get Height of Table Contents in Swift - Stack Overflow

PHOTO EMBED

Mon Aug 29 2022 12:11:14 GMT+0000 (Coordinated Universal Time)

Saved by @Raihan #swift

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
        }
    }
}
content_copyCOPY

https://stackoverflow.com/questions/38035346/get-height-of-table-contents-in-swift