0
votes

I have added a UIScrollView and wants to add 4 UITableView equal to screen size. So scrollview will have 4 pages equal to screen size. I have added a content view in scrollview and added 4 tableviews in it but I am not able to do vertical scroll probably because of its content size is not increasing.

Following is my xib:

enter image description here

The blue area is the content view but its width is not increasing although when I move the scrollview with page control I can see other tableviews.

1

1 Answers

1
votes

First of all, give static height of tableview and take this height outlet. And next step is , you need tableview observe for getting height then assign in this observer you can assign height of each 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
        }
    }

Note: make sure that you must need to disable scroll of all tableview