diff --git a/Source/Constraint.swift b/Source/Constraint.swift index 5d57e02..60e0f2f 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -193,15 +193,18 @@ internal class ConcreteConstraint: Constraint { } } + private let identifier: String? + private var installInfo: ConcreteConstraintInstallInfo? = nil - internal init(fromItem: ConstraintItem, toItem: ConstraintItem, relation: ConstraintRelation, constant: Any, multiplier: Float, priority: Float) { + internal init(fromItem: ConstraintItem, toItem: ConstraintItem, relation: ConstraintRelation, constant: Any, multiplier: Float, priority: Float, identifier: String? = nil) { self.fromItem = fromItem self.toItem = toItem self.relation = relation self.constant = constant self.multiplier = multiplier self.priority = priority + self.identifier = identifier } internal func installOnView(updateExisting updateExisting: Bool = false, file: String? = nil, line: UInt? = nil) -> [LayoutConstraint] { @@ -269,6 +272,7 @@ internal class ConcreteConstraint: Constraint { attribute: layoutToAttribute, multiplier: CGFloat(self.multiplier), constant: layoutConstant) + layoutConstraint.identifier = self.identifier // set priority layoutConstraint.priority = self.priority diff --git a/Source/ConstraintDescription.swift b/Source/ConstraintDescription.swift index 2fc8d84..6368fc2 100644 --- a/Source/ConstraintDescription.swift +++ b/Source/ConstraintDescription.swift @@ -34,6 +34,8 @@ public protocol ConstraintDescriptionFinalizable: class { var constraint: Constraint { get } + func identifiedAs(identifier: String) -> ConstraintDescriptionFinalizable + } /** @@ -191,6 +193,7 @@ internal class ConstraintDescription: ConstraintDescriptionExtendable, Constrain internal var centerX: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterX) } internal var centerY: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterY) } internal var baseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Baseline) } + internal var identifier: String? @available(iOS 8.0, *) internal var firstBaseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) } @@ -487,11 +490,17 @@ internal class ConstraintDescription: ConstraintDescriptionExtendable, Constrain relation: self.relation!, constant: self.constant, multiplier: self.multiplier, - priority: self.priority) + priority: self.priority, + identifier: self.identifier) } return self.concreteConstraint! } + func identifiedAs(identifier: String) -> ConstraintDescriptionFinalizable { + self.identifier = identifier + return self + } + // MARK: Private private let fromItem: ConstraintItem diff --git a/Tests/Tests.swift b/Tests/Tests.swift index e21c85d..7aa81c0 100644 --- a/Tests/Tests.swift +++ b/Tests/Tests.swift @@ -249,4 +249,17 @@ class SnapKitTests: XCTestCase { XCTAssertEqual(constraints[1].constant, 50, "Should be 50") } + func testConstraintIdentifier() { + let identifier = "Test-Identifier" + let view = View() + self.container.addSubview(view) + + view.snp_makeConstraints { (make) -> Void in + make.top.equalTo(self.container.snp_top).identifiedAs(identifier) + } + + let constraints = container.snp_constraints as! [NSLayoutConstraint] + XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'") + } + }