diff --git a/Source/Constraint.swift b/Source/Constraint.swift index e9abd75..5d43731 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -80,7 +80,42 @@ public class Constraint { for layoutFromAttribute in layoutFromAttributes { // get layout to attribute - let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute + let layoutToAttribute: NSLayoutAttribute + if layoutToAttributes.count > 1 { + if self.from.attributes == .edges && self.to.attributes == .margins { + switch layoutFromAttribute { + case .left: + layoutToAttribute = .leftMargin + case .right: + layoutToAttribute = .rightMargin + case .top: + layoutToAttribute = .topMargin + case .bottom: + layoutToAttribute = .bottomMargin + default: + fatalError() + } + } else if self.from.attributes == .margins && self.to.attributes == .edges { + switch layoutFromAttribute { + case .leftMargin: + layoutToAttribute = .left + case .rightMargin: + layoutToAttribute = .right + case .topMargin: + layoutToAttribute = .top + case .bottomMargin: + layoutToAttribute = .bottom + default: + fatalError() + } + } else { + layoutToAttribute = layoutToAttributes[0] + } + } else if layoutToAttributes.count == 1 { + layoutToAttribute = layoutToAttributes[0] + } else { + layoutToAttribute = layoutFromAttribute + } // get layout constant let layoutConstant: CGFloat = self.constant.constraintConstantTargetValueFor(layoutAttribute: layoutToAttribute) diff --git a/Source/ConstraintMakerRelatable.swift b/Source/ConstraintMakerRelatable.swift index c4141be..7a2968d 100644 --- a/Source/ConstraintMakerRelatable.swift +++ b/Source/ConstraintMakerRelatable.swift @@ -43,7 +43,9 @@ public class ConstraintMakerRelatable { if let other = other as? ConstraintItem { guard other.attributes == ConstraintAttributes.none || other.attributes.layoutAttributes.count <= 1 || - other.attributes.layoutAttributes == self.description.attributes.layoutAttributes else { + other.attributes.layoutAttributes == self.description.attributes.layoutAttributes || + other.attributes == .edges && self.description.attributes == .margins || + other.attributes == .margins && self.description.attributes == .edges else { fatalError("Cannot constraint to multiple non identical attributes. (\(file), \(line))"); } diff --git a/Tests/Tests.swift b/Tests/Tests.swift index 4d5bf4f..8b502b2 100644 --- a/Tests/Tests.swift +++ b/Tests/Tests.swift @@ -418,4 +418,44 @@ class SnapKitTests: XCTestCase { XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'") } + func testEdgesToMargins() { + var fromAttributes = Set() + var toAttributes = Set() + + let view = View() + self.container.addSubview(view) + + view.snp.remakeConstraints { (make) -> Void in + make.edges.equalTo(self.container.snp.margins) + } + + XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints") + + for constraint in (container.snp_constraints as! [NSLayoutConstraint]) { + fromAttributes.insert(constraint.firstAttribute) + toAttributes.insert(constraint.secondAttribute) + } + + XCTAssert(fromAttributes == [.top, .left, .bottom, .right]) + XCTAssert(toAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin]) + + fromAttributes.removeAll() + toAttributes.removeAll() + + view.snp.remakeConstraints { (make) -> Void in + make.margins.equalTo(self.container.snp.edges) + } + + XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints") + + for constraint in (container.snp_constraints as! [NSLayoutConstraint]) { + fromAttributes.insert(constraint.firstAttribute) + toAttributes.insert(constraint.secondAttribute) + } + + XCTAssert(toAttributes == [.top, .left, .bottom, .right]) + XCTAssert(fromAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin]) + + } + }