Skip to content

Commit b27e63b

Browse files
committed
Fix the width of the stack view by adding a constraint
for the center of the left image. This has the benefit of not requiring knowledge of the width of the image.
1 parent 3db2076 commit b27e63b

File tree

1 file changed

+4
-27
lines changed

1 file changed

+4
-27
lines changed

AutoLayout/AutoLayout/StackViewController.swift

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,33 +73,10 @@ class StackViewController: UIViewController {
7373
// Center both stack views
7474
addConstraintFromView(topStackView, attribute: .CenterX, multiplier: 1.0, identifier: "topSv center X")
7575
addConstraintFromView(bottomStackView, attribute: .CenterX, multiplier: 1.0, identifier: "bottomSV center X")
76-
77-
// Set the widths of each stack view by constraining
78-
// the leading edge
79-
if let heartTop = heartTop {
80-
let offset = heartTop.bounds.width/2
81-
let constraint = NSLayoutConstraint(item: topStackView,
82-
attribute: .Leading,
83-
relatedBy: .Equal,
84-
toItem: view,
85-
attribute: .CenterXWithinMargins,
86-
multiplier: 0.5,
87-
constant: -offset)
88-
constraint.identifier = "TopStackLeading"
89-
view.addConstraint(constraint)
90-
}
9176

92-
if let starBottomLeft = starBottomLeft {
93-
let offset = starBottomLeft.bounds.width/2
94-
let constraint = NSLayoutConstraint(item: bottomStackView,
95-
attribute: .Leading,
96-
relatedBy: .Equal,
97-
toItem: view,
98-
attribute: .CenterXWithinMargins,
99-
multiplier: 0.333,
100-
constant: -offset)
101-
constraint.identifier = "BottomStackLeading"
102-
view.addConstraint(constraint)
103-
}
77+
// Fix the width of the stack view by setting the center
78+
// of the left image.
79+
addConstraintFromView(heartTop, attribute: .CenterX, multiplier: 0.5, identifier: "heartTop center X")
80+
addConstraintFromView(starBottomLeft, attribute: .CenterX, multiplier: 0.333, identifier: "starBottomLeft center X")
10481
}
10582
}

0 commit comments

Comments
 (0)