Add support for edges to margins and margins to edges

This commit is contained in:
Robert Payne 2016-09-02 01:23:25 +12:00
parent 2f803657af
commit 514ec79042
3 changed files with 79 additions and 2 deletions

View File

@ -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)

View File

@ -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))");
}

View File

@ -418,4 +418,44 @@ class SnapKitTests: XCTestCase {
XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
}
func testEdgesToMargins() {
var fromAttributes = Set<NSLayoutAttribute>()
var toAttributes = Set<NSLayoutAttribute>()
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])
}
}