티스토리 뷰
[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.LeadingMargin
Middle Name Label.Leading = Superview.LeadingMargin
Last Name Label.Leading = Superview.LeadingMargin
First Name Text Field.Leading = First Name Label.Trailing + Standard
Middle Name Text Field.Leading = Middle Name Label.Trailing + Standard
Last Name Text Field.Leading = Last Name Label.Trailing + Standard
First Name Text Field.Trailing = Superview.TrailingMargin
Middle Name Text Field.Trailing = Superview.TrailingMargin
Last Name Text Field.Trailing = Superview.TrailingMargin
First Name Label.Baseline = First Name Text Field.Baseline
Middle Name Label.Baseline = Middle Name Text Field.Baseline
Last Name Label.Baseline = Last Name Text Field.Baseline
First Name Text Field.Width = Middle Name Text Field.Width
First Name Text Field.Width = Last Name Text Field.Width
First Name Text Field.Top = Top Layout Guide.Bottom + 20.0
Middle Name Text Field.Top = First Name Text Field.Bottom + Standard
Last Name Text Field.Top = Middle Name Text Field.Bottom + Standard
SuperView.Trailing Margin = Stack View.Trailing
Stack View.Leading = SuperView.LeadingMargin
Stack 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 |