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
}
}