Resize UIStackView after changing the number of UILabel rows

My iOS app has a tabular view with one UITableViewCell with the following layout (1 stack containing 2 labels and 1 button)

enter image description here

When the user clicks the button, the number of lines of the center label goes from 0 to 2 and looks like this:

enter image description here

Now there are two problems:

1) Resize UIStackView

2) Resizing a cell

I found a non-optimal solution for problem 1, which consists of adding an empty view to the stack. (invalidateIntrinsicContentSize doesn't work).

let emptyView = UIView()
emptyView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
stackView.addArrangedSubview(emptyView)

      

As you can see in the second screenshot, the cell does not resize and I am not sure if this is related to the stack view or the cell itself.

I would like to point out that I am writing code inside a subclass of UITableViewCell when the button event is handled in it.

For records, Tableview uses dynamic size:

// dynamic cell sizing 
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

    return CGFloat(cellEstimatedHeight)
}

      

+3


source to share


1 answer


UIStackView

it may or may not be your best bet ... it is more about organizing views within its own frame, rather than setting its own frame to views.

For a layout as simple as yours, it would be much better to just lay out the elements with normal constraints.

Take a look at this example: https://github.com/DonMag/DynamicCellHeight



Table B is one way to accomplish your layout (Table A was for a different layout I played with).

I needed a bit of trickery to keep the main shortcut in place while the cell size ... could probably find a better way. (Re-Edit - yep, found a better way)

Let me know if this makes sense - no doubt you'll want to do some tweaking.

0


source







All Articles