很简单观察者模式,直接上代码swift。
tableview.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
监听到变化后更新高度就可以了
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
var frame = tableview.frame
frame.size = tableview.contentSize
tableview.snp_updateConstraints { (make) in
make.height.equalTo(SCREEN_HEIGHT)
}
}
网友评论