Roger Harwood Roger Harwood - 4 months ago 26
Swift Question

How do i layout multiple text fields in a static table cell? (Xcode, swift)

I would like to make a view controller which acts as a form type page. For this i am inserting text fields and labels into a static table. I am having some trouble laying out the rows in the table which require multiple text fields. What is the best way to do this? Thank you in advance for any help.


Consider using a UIStackView as the top-level view within your table cell, with all four edges pinned to the edges of your cell's content view. Then you can add as many labels and fields as you need to the stack, which relieves you of all the constraint management while still providing a great deal of flexibility over their relative widths and spacing. For any views that you create and add programmatically, remember to turn off their translatesAutoresizingMaskIntoConstraints.

For more layout flexibility, you might also consider replacing the UITableView itself with a stack, giving you a vertical stack containing a series of horizontal stacks for each form item. If you need the form to be scrollable, you would need to place this stack inside a UIScrollView, and read up on the necessary constraints. (Summary: make sure to pin the width of the stack to the width of the enclosing scroll view, as well as all four of its edges.)