ios - UIScrollview 内容大小不会随着自动布局而增加

标签 ios swift uitableview uiscrollview autolayout

我已经添加了一个 UIScrollView,并想添加 4 个与屏幕大小相等的 UITableView。所以 scrollview 将有 4 个页面等于屏幕大小。我在 ScrollView 中添加了一个内容 View ,并在其中添加了 4 个表格 View ,但我无法进行垂直滚动,可能是因为它的内容大小没有增加。

以下是我的xib:

enter image description here

蓝色区域是内容 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/

相关文章:

ios - UIScrollView 中的 UIControll 未接收到触摸事件

ios - 动画 block 中 UIView 的旋转和设置中心有时不起作用

ios - 插入行时单元格格式不正确

ios - 在 XCode 中更改项目文件夹名称后出现错误

c# - 如何在 iOS 上的 Google Play 游戏服务中禁用推送通知权限?

ios - 如何回滚到 Xcode 6.4,Carthage

ios - 如何让 Siri 在特定关键字上启动应用程序?

ios - UITableViewCell 覆盖 : when alloc UILabel in cellForRowAtIndexPath?

ios - UITableView HeaderView 上的 SearchDisplayController 怪异行为

iphone - uitableviewcell 中的 UIStepper