美文网首页
UITableView 动态获取高度

UITableView 动态获取高度

作者: 麦志超 | 来源:发表于2023-08-23 16:19 被阅读0次
    self.tableView.addObserver(self, forKeyPath: "contentSize", options: [.new, .old, .prior], context: nil)


    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "contentSize" {
            if let oldValue = change?[NSKeyValueChangeKey.oldKey] as? CGSize, let newValue = change?[NSKeyValueChangeKey.newKey] as? CGSize {
                if oldValue.height != newValue.height {
                    tableView.snp.updateConstraints { make in
                        make.height.equalTo(newValue.height)
                    }
                }
            }
        }
    }

相关文章

网友评论

      本文标题:UITableView 动态获取高度

      本文链接:https://www.haomeiwen.com/subject/nubpmdtx.html