Merge pull request #242 from ello/develop

Exposes "native constraints"
This commit is contained in:
Robert Payne 2016-06-22 10:02:33 +12:00 committed by GitHub
commit 307d03ec43
2 changed files with 36 additions and 0 deletions

View File

@ -57,6 +57,8 @@ public class Constraint {
public func updatePriorityHigh() -> Void { fatalError("Must be implemented by Concrete subclass.") } public func updatePriorityHigh() -> Void { fatalError("Must be implemented by Concrete subclass.") }
public func updatePriorityMedium() -> Void { fatalError("Must be implemented by Concrete subclass.") } public func updatePriorityMedium() -> Void { fatalError("Must be implemented by Concrete subclass.") }
public func updatePriorityLow() -> Void { fatalError("Must be implemented by Concrete subclass.") } public func updatePriorityLow() -> Void { fatalError("Must be implemented by Concrete subclass.") }
public var layoutConstraints: [LayoutConstraint] { fatalError("Must be implemented by Concrete subclass.") }
internal var makerFile: String = "Unknown" internal var makerFile: String = "Unknown"
internal var makerLine: UInt = 0 internal var makerLine: UInt = 0
@ -197,6 +199,17 @@ internal class ConcreteConstraint: Constraint {
private var installInfo: ConcreteConstraintInstallInfo? = nil private var installInfo: ConcreteConstraintInstallInfo? = nil
override var layoutConstraints: [LayoutConstraint] {
if installInfo == nil {
install()
}
guard let installInfo = installInfo else {
return []
}
return installInfo.layoutConstraints.allObjects as! [LayoutConstraint]
}
internal init(fromItem: ConstraintItem, toItem: ConstraintItem, relation: ConstraintRelation, constant: Any, multiplier: Float, priority: Float, label: String? = nil) { internal init(fromItem: ConstraintItem, toItem: ConstraintItem, relation: ConstraintRelation, constant: Any, multiplier: Float, priority: Float, label: String? = nil) {
self.fromItem = fromItem self.fromItem = fromItem
self.toItem = toItem self.toItem = toItem

View File

@ -291,4 +291,27 @@ class SnapKitTests: XCTestCase {
XCTAssertEqual(constraints[0].constant, 10, "Should be 10") XCTAssertEqual(constraints[0].constant, 10, "Should be 10")
XCTAssertEqual(constraints[1].constant, -10, "Should be 10") XCTAssertEqual(constraints[1].constant, -10, "Should be 10")
} }
func testNativeConstraints() {
let view = View()
container.addSubview(view)
var topNativeConstraints: [LayoutConstraint]!
var topNativeConstraint: LayoutConstraint?
var sizeNativeConstraints: [LayoutConstraint]!
view.snp_makeConstraints { (make) -> Void in
let topConstraint = make.top.equalToSuperview().inset(10).constraint
topNativeConstraints = topConstraint.layoutConstraints
topNativeConstraint = topConstraint.layoutConstraints.first
let sizeConstraints = make.size.equalTo(50).constraint
sizeNativeConstraints = sizeConstraints.layoutConstraints
}
XCTAssertEqual(topNativeConstraints.count, 1, "make.top should creates one native constraint")
XCTAssertEqual(topNativeConstraint?.constant, 10, "topNativeConstraint.constant is set to 10")
XCTAssertEqual(sizeNativeConstraints.count, 2, "make.tosize should create two native constraint")
XCTAssertEqual(sizeNativeConstraints[0].constant, 50, "sizeNativeConstraints should set size[0] to 50")
XCTAssertEqual(sizeNativeConstraints[1].constant, 50, "sizeNativeConstraints should set size[1] to 50")
}
} }