【问题标题】:UIScrollview content size is not increasing with auto layoutUIScrollview 内容大小不会随着自动布局而增加
【发布时间】:2019-01-05 09:28:45
【问题描述】:

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

以下是我的 xib:

蓝色区域是内容视图,但它的宽度没有增加,尽管当我使用页面控件移动滚动视图时,我可以看到其他表格视图。

【问题讨论】:

    标签: ios swift uitableview uiscrollview autolayout


    【解决方案1】:

    首先,给定tableview的静态高度,取这个高度出口。下一步是,您需要 tableview 观察来获取高度,然后在这个观察者中分配您可以分配每个 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的滚动

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      相关资源
      最近更新 更多