1

I have a scrollView that contains table view, image view and many labels. Here is the sample location of the controls:

  • Image View1
  • Table View
  • Label 1
  • Label 2
  • Label 3
  • Label 4
  • Image View 2

The table is is non-scrollable. I managed to changed its height based on the number of rows. Here is the code I used an I put it after the cell is created:

var frame = tableView.frame
frame.size.height = tableView.contentSize.height
tableView.frame = frame

However, when the Table view height adjusts, the location of other controls (Label1, Label2, Label3, Label4 and Image View2) did not adjust. They overlapped. How to auto adjust the location of other controls when table view height adjusts?

UPDATE: viewController code:

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    @IBOutlet weak var _Label1: UILabel!
    @IBOutlet weak var _Label2: UILabel!
    @IBOutlet weak var _Label3: UILabel!
    @IBOutlet weak var _Label4: UILabel!
    @IBOutlet weak var _Label5: UILabel!

    @IBOutlet weak var _TableViewItems: UITableView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // this will remove the extra empty cell divider lines
        _TableViewItems.tableFooterView = UIView()

        _TableViewItems.delegate = self
        _TableViewItems.dataSource = self

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "sampleId") as! Sample
        let row = indexPath.row

        var frame = _TableViewItems.frame

        cell ._LabelNext.text = "Sample Text"

        frame.size.height = _TableViewItems.contentSize.height
        _TableViewItems.frame = frame

        return cell
    }

    override func viewWillLayoutSubviews()
    {
        super.viewWillLayoutSubviews()
        tableViewHeightConstraint.constant = _TableViewItems.contentSize.height // <------ unresolved tableViewHeightConstraint
    }
}
user123456
  • 73
  • 1
  • 8
  • You need to setup right constraints inside of your scrollview. You may see example in this article https://www.ralfebert.de/ios-examples/auto-layout/uiscrollview-storyboard/ – gaRik Aug 30 '18 at 08:13
  • Possible issues you may check here https://stackoverflow.com/questions/43614008/getting-scrollview-to-work-with-autolayout-and-storyboard. – gaRik Aug 30 '18 at 08:20
  • put all things in a stack view scroll view -> stack view -> img table etc.. – Harshil Kotecha Aug 30 '18 at 09:37

2 Answers2

6

Not sure where you have added mentioned code

But, using constraints (autolayout) and adding following code will solve your issue.

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    tableViewHeightConstraint.constant = tableView.contentSize.height
}
Satish
  • 2,015
  • 1
  • 14
  • 22
0

You Controls are overlapping because you need to adjust the frame of other controls after you are doing this.

var frame = tableView.frame
frame.size.height = tableView.contentSize.height
tableView.frame = frame
// Adjust Frame of other controls below tableview.

But I strongly recommend you to use Constraints. if you don't know about constraints read this.