티스토리 뷰
[iOS Autolayout] Why stackViews first, constraints later
Stack views provide an easy way to leverage the power of Auto Layout without introducing the complexity of constraints.
In general, use stack views to manage as much of your layout as possible. Resort to creating constraints only when you cannot achieve your goals with stack views alone.
The following recipes show how you can use stack views to create layouts of increasing complexity. Stack views are a powerful tool for quickly and easily designing your user interfaces. Their attributes allow a high degree of control over how they lay out their arranged views. You can augment these settings with additional, custom constraints; however, this increases the layout’s complexity.
from apple auto layout guide document
위와 같은 레이아웃을 만들 때
only contraints
stackview & constrains
stacview를 쓰지 않고 constraints만 쓴다면, 아래와 같이 17개의 constraints를 써야 합니다.
First Name Label.Leading = Superview.LeadingMarginMiddle Name Label.Leading = Superview.LeadingMarginLast Name Label.Leading = Superview.LeadingMarginFirst Name Text Field.Leading = First Name Label.Trailing + StandardMiddle Name Text Field.Leading = Middle Name Label.Trailing + StandardLast Name Text Field.Leading = Last Name Label.Trailing + StandardFirst Name Text Field.Trailing = Superview.TrailingMarginMiddle Name Text Field.Trailing = Superview.TrailingMarginLast Name Text Field.Trailing = Superview.TrailingMarginFirst Name Label.Baseline = First Name Text Field.BaselineMiddle Name Label.Baseline = Middle Name Text Field.BaselineLast Name Label.Baseline = Last Name Text Field.BaselineFirst Name Text Field.Width = Middle Name Text Field.WidthFirst Name Text Field.Width = Last Name Text Field.WidthFirst Name Text Field.Top = Top Layout Guide.Bottom + 20.0Middle Name Text Field.Top = First Name Text Field.Bottom + StandardLast Name Text Field.Top = Middle Name Text Field.Bottom + Standard
SuperView.Trailing Margin = Stack View.TrailingStack View.Leading = SuperView.LeadingMarginStack View.top = Top Laout Guide.Bottom + 20
이렇게 stackView 3개와 3개의 constraint만 쓰면 됩니다. constraints의 숫자도 약 6배 정도 차이 나지만 그 뿐만 아니라 관리하기도 편합니다.
예를 들어 label과 textField 사이의 공간을 조정하려면 constraint만 쓴 건 3개의 constraint를 수정해야 하지만 stackview를 쓴 건 stacview에 spacing(CGFloat) 하나만 조절해주면 됩니다.
'iOS' 카테고리의 다른 글
| [IOS AUTO LAYOUT] Dynamic Stack View (0) | 2018.07.18 |
|---|---|
| [IOS AUTOLAYOUT] Nested Stack Views (0) | 2018.07.14 |
| [ios autolayout] simple stackview (0) | 2018.07.11 |
| [ios autolayout ]What is the StackView (0) | 2018.07.05 |
| iOS AutoLayout (0) | 2018.05.20 |
