4

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aman.Samghani
  • 2,151
  • 3
  • 12
  • 27

3 Answers3

11
  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.

Priya
  • 735
  • 5
  • 10
  • 3
    it doesn't mean that you can club only same views in stack view. even if you want textfield, label and buttons allinged on a row/column then also you can use stack view. **Best use of stack view is that if you want to set multiple controls allinged vertically or horizontally to each other you just add all of them in stack view. stackview will handle its allignment you just need to give frame contraints to stackView.** – Rishi Chaurasia Nov 10 '17 at 12:33
4

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

Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55
  • Can we give dynamic spacing between two objects? i.e It should change as per device width changes. – Aman.Samghani Nov 10 '17 at 11:57
  • Yes. You constrain the stack view, just as you would other UI elements. It's just with the stack view you will set fewer constraints, so it's easier to manage. – Martin Muldoon Nov 10 '17 at 11:59
  • In the image above you see a Distribution property in Attribute Inspector. You use this setting to have a spacing change dynamically as you like. – Martin Muldoon Nov 10 '17 at 12:11
0

UIStackView helps you to set autolayout. It isn't neccessary!