UITableView 动态获取高度

2023-08-23  本文已影响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)
                    }
                }
            }
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读