Swift has kVO’s ability to monitor attribute changes built into it. We often need to monitor changes to certain properties, such as contentSize changes during the first drop-down refresh. The following code can be copied directly:
private func addObserver() { tableView? .addObserver(self,forKeyPath: "contentSize", options: .new, context: nil)
}
private func removeAbserver() { tableView? .removeObserver(self,forKeyPath:"contentSize")
}
var curentContentHeight : CGFloat = 0
override open func observeValue(forKeyPath keyPath: String? , of object: Any? , change: [NSKeyValueChangeKey : Any]? , context: UnsafeMutableRawPointer?) { guard tableView.isUserInteractionEnabledelse {
return
}
print(tableView.contentSize.height)
curentContentHeight = tableView.contentSize.height
self.frame.origin.y = curentContentHeight
}Copy the code
This way, there is no need to use third-party libraries (such as KVOController), which is very easy to test.