From b3b64f61a74572bab2c36eced63f954f3a71e587 Mon Sep 17 00:00:00 2001 From: Christian Schnorr Date: Tue, 2 Apr 2019 19:31:01 +0200 Subject: [PATCH] Fix incorrect behavior of inset with center and baseline anchors (#569) * Fix insets adjusting center attributes incorrectly * Fix insets adjusting baseline attributes incorrectly --- Source/ConstraintConstantTarget.swift | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Source/ConstraintConstantTarget.swift b/Source/ConstraintConstantTarget.swift index 7224df9..24052ae 100644 --- a/Source/ConstraintConstantTarget.swift +++ b/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: