Skip to content

Commit b3b64f6

Browse files
jenoxrobertjpayne
authored andcommitted
Fix incorrect behavior of inset with center and baseline anchors (SnapKit#569)
* Fix insets adjusting center attributes incorrectly * Fix insets adjusting baseline attributes incorrectly
1 parent bc2b0b9 commit b3b64f6

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Source/ConstraintConstantTarget.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,22 @@ extension ConstraintConstantTarget {
106106
if let value = self as? ConstraintInsets {
107107
#if os(iOS) || os(tvOS)
108108
switch layoutAttribute {
109-
case .left, .leftMargin, .centerX, .centerXWithinMargins:
109+
case .left, .leftMargin:
110110
return value.left
111-
case .top, .topMargin, .centerY, .centerYWithinMargins, .lastBaseline, .firstBaseline:
111+
case .top, .topMargin, .firstBaseline:
112112
return value.top
113113
case .right, .rightMargin:
114114
return -value.right
115-
case .bottom, .bottomMargin:
115+
case .bottom, .bottomMargin, .lastBaseline:
116116
return -value.bottom
117117
case .leading, .leadingMargin:
118118
return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
119119
case .trailing, .trailingMargin:
120120
return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
121+
case .centerX, .centerXWithinMargins:
122+
return (value.left - value.right) / 2
123+
case .centerY, .centerYWithinMargins:
124+
return (value.top - value.bottom) / 2
121125
case .width:
122126
return -(value.left + value.right)
123127
case .height:
@@ -131,18 +135,22 @@ extension ConstraintConstantTarget {
131135
}
132136
#else
133137
switch layoutAttribute {
134-
case .left, .centerX:
138+
case .left:
135139
return value.left
136-
case .top, .centerY, .lastBaseline, .firstBaseline:
140+
case .top, .firstBaseline:
137141
return value.top
138142
case .right:
139143
return -value.right
140-
case .bottom:
144+
case .bottom, .lastBaseline:
141145
return -value.bottom
142146
case .leading:
143147
return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
144148
case .trailing:
145149
return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
150+
case .centerX:
151+
return (value.left - value.right) / 2
152+
case .centerY:
153+
return (value.top - value.bottom) / 2
146154
case .width:
147155
return -(value.left + value.right)
148156
case .height:

0 commit comments

Comments
 (0)