Browse Source

Fix incorrect behavior of inset with center and baseline anchors (#569)

* Fix insets adjusting center attributes incorrectly

* Fix insets adjusting baseline attributes incorrectly
Christian Schnorr 6 years ago
parent
commit
b3b64f61a7
1 changed files with 14 additions and 6 deletions
  1. 14 6
      Source/ConstraintConstantTarget.swift

+ 14 - 6
Source/ConstraintConstantTarget.swift

@@ -106,18 +106,22 @@ extension ConstraintConstantTarget {
         if let value = self as? ConstraintInsets {
             #if os(iOS) || os(tvOS)
                 switch layoutAttribute {
-                case .left, .leftMargin, .centerX, .centerXWithinMargins:
+                case .left, .leftMargin:
                     return value.left
-                case .top, .topMargin, .centerY, .centerYWithinMargins, .lastBaseline, .firstBaseline:
+                case .top, .topMargin, .firstBaseline:
                     return value.top
                 case .right, .rightMargin:
                     return -value.right
-                case .bottom, .bottomMargin:
+                case .bottom, .bottomMargin, .lastBaseline:
                     return -value.bottom
                 case .leading, .leadingMargin:
                     return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
                 case .trailing, .trailingMargin:
                     return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
+                case .centerX, .centerXWithinMargins:
+                    return (value.left - value.right) / 2
+                case .centerY, .centerYWithinMargins:
+                    return (value.top - value.bottom) / 2
                 case .width:
                     return -(value.left + value.right)
                 case .height:
@@ -131,18 +135,22 @@ extension ConstraintConstantTarget {
             }
             #else
                 switch layoutAttribute {
-                case .left, .centerX:
+                case .left:
                     return value.left
-                case .top, .centerY, .lastBaseline, .firstBaseline:
+                case .top, .firstBaseline:
                     return value.top
                 case .right:
                     return -value.right
-                case .bottom:
+                case .bottom, .lastBaseline:
                     return -value.bottom
                 case .leading:
                     return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
                 case .trailing:
                     return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
+                case .centerX:
+                    return (value.left - value.right) / 2
+                case .centerY:
+                    return (value.top - value.bottom) / 2
                 case .width:
                     return -(value.left + value.right)
                 case .height: