Dynamcially fit height of tableview according to its cells content into the scrollview

Related searches

I want to Fit A UIVIew and a Tableview in a ScrollView But The table view Height is not Dynamically increasing according to the avaualable cells.

What I have done is.

  1. Gave TableView Height constant and priority to 999
  2. Made an outlet of the table view height content and added this code
 self.tableViewHeightConstraint.constant = self.myNewTableView.contentSize.height
 self.view.layoutIfNeeded()
  1. Made tablview scrollable to false

But the TableView Height is not increasing according to cells

Here is the Example code of My Code.

TableviewCell:-

import UIKit

class NewTableViewCell: UITableViewCell {

    @IBOutlet weak var testLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

ViewController:-

import UIKit

class NewViewViewController: UIViewController {

    @IBOutlet weak var myNewTableView: UITableView!
    @IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        myNewTableView.rowHeight = 60
        myNewTableView.estimatedRowHeight = UITableView.automaticDimension

    }


    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

    }

}

extension NewViewViewController:UITableViewDelegate,UITableViewDataSource{


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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewTableViewCell", for: indexPath) as! NewTableViewCell
        cell.testLabel.text = "Testing the height of tableview"

        self.tableViewHeightConstraint.constant = self.myNewTableView.contentSize.height
        self.view.layoutIfNeeded()

        return cell
    }


}

Your self.tableViewHeightConstraint is not connected to the storyboard tableView. I think you mistakenly deleted it in storyBoard. After connecting this constraint, it's working fine. Please check the following attachments.

After this change Output:-

How to dynamically increase height of scrollview based on uitableview, ableview in a ScrollView But The table view Height is not Dynamically increasing according to the avaualable cells. What I have done is. To change the height of tableView cell in ios dynamically, i.e resizing the cell according to the content available, we’ll need to make use of automatic dimension property. We’ll see this with the help of an sample project. Create an empty project and go to it’s viewController class, conform it to UITableViewDataSource and


You need to add it here

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.tableViewHeightConstraint.constant = self.myNewTableView.contentSize.height
    self.view.layoutIfNeeded()
}

Self-sizing Table View Cells, In that page, UIScrollView height will be dynamically increased based on your constraint of cells subView must set to top to bottom in order to calculate on the number of cell (with different height based on content) it has to display. Any ideas on how to ensure View B's height will adjust to tableview (BTW, each row is a� To do this, you need to configure the table with the.rowHeight = UITableViewAutomaticDimension property to automatically determine the row height, and set the estimatedRowHeight property to an estimate of the row height. This doesn’t need to be exact - the height for visible cells is determined by the actual size.


I think the below code will help you.

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    self.YOUR_TABLEVIEW.layoutIfNeeded()
    self.YOUR_TABLEVIEW_HEIGHTCONSTRAINT.constant = self.YOUR_TABLEVIEW.contentSize.height
}

Estimating the Height of a Table's Scrolling Area, “Our table view cells have to resize (gulp!) dynamically! Your cell heights will need to be dynamic, based on the content of each cell. grow its size to fit all of its content, since the user won't be able to scroll through the text. This results in a square cell, which is useful for heat maps. Text Cell – Adjusts the view so the cell has a 3:1 aspect ratio. This is useful for text tables. For example, you can see the text table below with Square Cell and Text Cell applied. Text Cell enforces a cell aspect ratio of 3:1 and results in a compact table that is easy to read.


Configuring the Cells for Your Table, Provide height estimates for your table view's headers, footers, and rows to ensure that scrolling accurately reflects the size of your content. Whenever possible, table views use height estimates for cells, headers, and footers to improve The table view asks for estimates for every item in your table, so do not perform� Loading Table View Cells from a Storyboard. In a storyboard, the cells in a table view are dynamic or static. With dynamic content, the table view is a list with a large (and potentially unbounded) number of rows. With static content, the number of rows is a finite quantity that’s known at compile time.


In your tableView(_:cellForRowAt:) method, configure the content of your cell scroll views, table views, collection views, or other complex container views in cells. If the row heights are not all the same, or can change dynamically, provide the use system spacing constraints to adjust the spacing surrounding text labels. Xamarin.Forms on iOS does not yet support automatically adjusting the height of cells, you need to manually set the height of rows (their Height property) as well as set HasUnevenRows = true. This is tricky because there's no easy way to calculate the required height - you end up having to estimate. It's come up a few times on the forums.


If you have set the row height in row definitions, the use that value. Else, get the height of the controls which are in the data template. In my example, i have added a gridview in the view cell of data template. The grid has two rows and there is a image with rowspan=2. So the height of the data template is the height of the image in the