https://github.com/yzhong52/autosizestackview
How to size a UIStackView depending on its content
https://github.com/yzhong52/autosizestackview
Last synced: 6 months ago
JSON representation
How to size a UIStackView depending on its content
- Host: GitHub
- URL: https://github.com/yzhong52/autosizestackview
- Owner: yzhong52
- Created: 2017-12-02T14:37:12.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-02-21T21:24:17.000Z (over 4 years ago)
- Last Synced: 2025-03-20T16:52:44.704Z (7 months ago)
- Language: Swift
- Size: 10.7 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Autosize Stackview Demo
## Setup
`UIStackView` is designed to grow its size according to the content. In order for that to work, you need to set up the appropriate constraints between the `UIStackView` and the `UITableViewCell`. For example, this is how the constraints look like in interface builder:
[![enter image description here][1]][1]
[![enter image description here][2]][2]If you like setting up constraints in code, that should work too. Just have to add the constraints to the four sides: leading, trailing, top and bottom.
## Result
To demonstrate the result, I have this as the `cellForRowAt` function. Basically, it puts a number of `UILabel` inside the `stackView` and the label count is depending on the row number (i.e. one label in the 1st row, two labels in the 2nd row, and etc.).
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:
[1]: https://i.stack.imgur.com/XtE3t.png
[2]: https://i.stack.imgur.com/9rzzJ.png
[3]: https://i.stack.imgur.com/04vVT.png