我已经添加了一个 UIScrollView,并想添加 4 个与屏幕大小相等的 UITableView。所以 scrollview 将有 4 个页面等于屏幕大小。我在 ScrollView 中添加了一个内容 View ,并在其中添加了 4 个表格 View ,但我无法进行垂直滚动,可能是因为它的内容大小没有增加。
以下是我的xib:
蓝色区域是内容 View ,但它的宽度没有增加,尽管当我使用页面控件移动 ScrollView 时,我可以看到其他表格 View 。
最佳答案
首先,给定tableview的静态高度,取这个高度outlet。下一步是,你需要 tableview observe 来获取高度,然后在这个观察者中分配你可以分配每个 tableview 的高度。
override func viewWillAppear(_ animated: Bool) {
Tblvw1.addObserver(self, forKeyPath: "contentSize", options: [.new], context: nil)
Tblvw2.addObserver(self, forKeyPath: "contentSize", options: [.new], context: nil)
Tblvw3.addObserver(self, forKeyPath: "contentSize", options: [.new], context: nil)
Tblvw4.addObserver(self, forKeyPath: "contentSize", options: [.new], context: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Tblvw1.removeObserver(self, forKeyPath: "contentSize")
Tblvw2.removeObserver(self, forKeyPath: "contentSize")
Tblvw3.removeObserver(self, forKeyPath: "contentSize")
Tblvw4.removeObserver(self, forKeyPath: "contentSize")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if object is UITableView {
print("contentSize:= \(Tblvw1.contentSize.height)")
self.heightOfTable1.constant = Tblvw1.contentSize.height
self.heightOfTable2.constant = Tblvw2.contentSize.height
self.heightOfTable3.constant = Tblvw3.contentSize.height
self.heightOfTable4.constant = Tblvw4.contentSize.height
}
}
注意:确保您必须禁用所有tableview的滚动
关于ios - UIScrollview 内容大小不会随着自动布局而增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54050695/