hugging How to size a UIStackView depending on its content?



uistackview programmatically swift 4 (1)

UIStackView is designed to grow its size according to the content. In order for that to work, you need to set up the constraints between the UIStackView and the UITableViewCell. For example, this is how the constraints look like in interface builder:

If you like setting up constraints in code, that should work too.

To demonstrate that this will work, I have this for the cellForRowAt function. Basically, it puts a number of UILabel inside the stackView and the label count is depending on the row number.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableviewCell", for: indexPath) as! TableviewCell
    for i in 1...indexPath.row + 1 {
        let label = UILabel()
        label.text = "Row \(indexPath.row), Label \(i)"
        cell.stackView.addArrangedSubview(label)
    }
    return cell
}

Here is the final result:

https://github.com/yzhong52/AutosizeStackview


Edit

For those who like doing everything in code instead of interface builder:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableviewCell", for: indexPath)
    let stackview = UIStackView()
    stackview.translatesAutoresizingMaskIntoConstraints = false
    stackview.axis = .vertical
    cell.addSubview(stackview)
    NSLayoutConstraint.activate([
        stackview.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 10),
        stackview.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -10),
        stackview.topAnchor.constraint(equalTo: cell.topAnchor, constant: 10),
        stackview.bottomAnchor.constraint(equalTo: cell.bottomAnchor, constant: -10),
        ])

    for i in 1...indexPath.row + 1 {
        let label = UILabel()
        label.text = "Row \(indexPath.row), Label \(i)"
        stackview.addArrangedSubview(label)
    }
    return cell
}

https://github.com/yzhong52/AutosizeStackview/tree/code_instead_of_interface_builder

I would like to have a similar behavior than the <Table> HTML tag, in the sense where the frame is sized according to its content.

In my very context, I use an UIStackView as the content view of a UITableViewCell. As items in the cell are various information, the resulting height of the cell should be variable.

My strategy is to programmatically build a cell as a UIStackView with a .Vertical axis, as in the code snippet as follows:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    let sv = UIStackView(frame: tableview.frame)
    sv.axis = .Vertical
    cell.addSubview(sv)
    for i in information {
        let l = UILabel()
        l.text = i
        sv.addSubViewAndArrange(l)
    }

    return cell
}

Unfortunately, the cell size does not adjust to the content, and as a result I have to set the cell height myself, as follows:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return cellHeight     // a constant
}

How could I fix that?