8

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?

enter image description here

4

2 回答 2

8

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.

于 2016-03-29T13:10:08.770 回答
7

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:

  1. Put your labels in one vertically aligned stackview and
  2. Put your text fields in another vertically aligned stackview
  3. 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.)

于 2015-10-07T17:20:51.373 回答