Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should we use UIStackView?

I am designing iOS Application since 2 years. I have designed 8-10 applications with using UIScrollView, UICollectionView and many more native views. But I have never used UIStackView. I have gone through many documents of UIStackView, but I am not able to identify exact situation where i can use UIStackView.

Can anyone guide me for same?

Thank you in advance.

like image 678
Aman.Samghani Avatar asked Dec 07 '22 16:12

Aman.Samghani


2 Answers

  1. UIStackView is useful when you need to repeat same views multiple times like in Sing up view. We use many textfields and manually set constraints between each textfields. But if you put all textfields in stack view then you just need to set required constraints of stackview only and not textfields. Textfields inside stackview will be arranged automatically without Autolayout.

  2. Sometimes we need to hide view and we want to remove its occupied space so at that time use of stackview is recommended because if you hide any view that resided in stackview will also remove its occupied space automatically.

like image 174
Priya Avatar answered Dec 11 '22 12:12

Priya


I believe you mean UIStackView. You use a UIStackView to group UI elements together to simplify setting constraints. In other words once you've added objects to the stack view, you constrain the stackview and set attributes on it rather than on each individual element. This results in far fewer constraints.

In this example, I've added a label and a text field to a stack view. Then I added constraints to the stack view and set the spacing in Attributes Inspector to 20.

If I had to constrain the label and the text field I'd have many more constraints.

enter image description here

like image 33
Martin Muldoon Avatar answered Dec 11 '22 12:12

Martin Muldoon