I'm creating a basic form with various fields. I'd like to use UIStackView
to layout all the controls. How would I do the following so the controls are aligned correctly? Is the UIStackView
the correct way to do this?
2 回答
Apple suggests creating horizontal stack views with each label and field, and then putting them in a vertical stack view.
This makes sense in that if you later change one of controls to a different control (say a popup button), you can keep the label and control baselines aligned.
You can definitely use StackViews to achieve this.
How you arrange your StackViews depends on how you want to manage your alignment.
Here's what I recommend:
- Put your labels in one vertically aligned stackview and
- Put your text fields in another vertically aligned stackview
- Put the two stackviews in a horizontally aligned stackview
You'll likely want to set your distribution
to fill equally on the two smaller stackviews, and then set the spacing
to be the same for each smaller stackview.
Finally, you'll be able to set the spacing on the horizontal stack view to set the space between your text and your textfields. (That final part the reason I would set them up like this.)