Augusto
Augusto

Reputation: 4253

How to automatic set constraints to make a uniform distance between UIViews?

I've a ViewController that have some components, UILabels and UITextFields. I wants that the distance between them is uniform, 2 Points Top and Bottom, for example. So far, I adjust manually the distance. I wants know if exists a way to make this automatically. I tried select all UIViews and set constraints, but not works.

Actually layout:

enter image description here

When I tried set all components constraints:

enter image description here

Not works perfectelly:

enter image description here

Upvotes: 0

Views: 87

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100549

You can't set that constant spacing by selecting all components , but you can insert them inside a Vertical UIStackview (emebded in a UIScrollView ) with distribution fillEqually and set spacing between items to any constant you want

-> mainView
   -> scrollView
      ->contentView   
         ->stackview
            ->lbl1
            ->txtf1

Upvotes: 2

pacification
pacification

Reputation: 6018

I wants that the distance between them is uniform, 2 Points Top and Bottom, for example.

For this kind of work UIStackView do the best job. Simply select all view, check Embed In Stack:

enter image description here

And then specify the Spacing value (note, that sometimes you also need to change the Axis parameter, but in most cases initial value is correct for your ui elements):

enter image description here

Result (distance between all labels is 8):

enter image description here

Upvotes: 1

Related Questions