Constraint.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. //
  2. // Snap
  3. //
  4. // Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #if os(iOS)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. public protocol FloatConstantConstraint {
  29. func update(float: Float)
  30. }
  31. public protocol DoubleConstantConstraint {
  32. func update(float: Float)
  33. }
  34. public protocol CGFloatConstantConstraint {
  35. func update(float: Float)
  36. }
  37. public protocol IntConstantConstraint {
  38. func update(float: Float)
  39. }
  40. public protocol UIntConstantConstraint {
  41. func update(float: Float)
  42. }
  43. public protocol CGPointConstantConstraint {
  44. func update(float: Float)
  45. }
  46. public protocol CGSizeConstantConstraint {
  47. func update(size: CGSize)
  48. }
  49. public protocol EdgeInsetsConstantConstraint {
  50. func update(insets: EdgeInsets)
  51. }
  52. /**
  53. * Constraint is a single item that defines all the properties for a single ConstraintMaker chain
  54. */
  55. public class Constraint {
  56. public var left: Constraint { return addConstraint(ConstraintAttributes.Left) }
  57. public var top: Constraint { return addConstraint(ConstraintAttributes.Top) }
  58. public var right: Constraint { return addConstraint(ConstraintAttributes.Right) }
  59. public var bottom: Constraint { return addConstraint(ConstraintAttributes.Bottom) }
  60. public var leading: Constraint { return addConstraint(ConstraintAttributes.Leading) }
  61. public var trailing: Constraint { return addConstraint(ConstraintAttributes.Trailing) }
  62. public var width: Constraint { return addConstraint(ConstraintAttributes.Width) }
  63. public var height: Constraint { return addConstraint(ConstraintAttributes.Height) }
  64. public var centerX: Constraint { return addConstraint(ConstraintAttributes.CenterX) }
  65. public var centerY: Constraint { return addConstraint(ConstraintAttributes.CenterY) }
  66. public var baseline: Constraint { return addConstraint(ConstraintAttributes.Baseline) }
  67. public var and: Constraint { return self }
  68. public var with: Constraint { return self }
  69. // MARK: initializer
  70. internal init(fromItem: ConstraintItem) {
  71. self.fromItem = fromItem
  72. self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
  73. }
  74. // MARK: equalTo
  75. public func equalTo(other: ConstraintItem) -> Constraint {
  76. return constrainTo(other, relation: .Equal)
  77. }
  78. public func equalTo(other: View) -> Constraint {
  79. return constrainTo(other, relation: .Equal)
  80. }
  81. #if os(iOS)
  82. public func equalTo(other: UILayoutSupport) -> Constraint {
  83. return constrainTo(other, relation: .Equal)
  84. }
  85. #endif
  86. public func equalTo(other: Float) -> Constraint {
  87. return constrainTo(other, relation: .Equal)
  88. }
  89. public func equalTo(other: Double) -> Constraint {
  90. return constrainTo(Float(other), relation: .Equal)
  91. }
  92. public func equalTo(other: CGFloat) -> Constraint {
  93. return constrainTo(Float(other), relation: .Equal)
  94. }
  95. public func equalTo(other: Int) -> Constraint {
  96. return constrainTo(Float(other), relation: .Equal)
  97. }
  98. public func equalTo(other: UInt) -> Constraint {
  99. return constrainTo(Float(other), relation: .Equal)
  100. }
  101. public func equalTo(other: CGSize) -> Constraint {
  102. return constrainTo(other, relation: .Equal)
  103. }
  104. public func equalTo(other: CGPoint) -> Constraint {
  105. return constrainTo(other, relation: .Equal)
  106. }
  107. public func equalTo(other: EdgeInsets) -> Constraint {
  108. return constrainTo(other, relation: .Equal)
  109. }
  110. // MARK: lessThanOrEqualTo
  111. public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint {
  112. return constrainTo(other, relation: .LessThanOrEqualTo)
  113. }
  114. public func lessThanOrEqualTo(other: View) -> Constraint {
  115. return constrainTo(other, relation: .LessThanOrEqualTo)
  116. }
  117. #if os(iOS)
  118. public func lessThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  119. return constrainTo(other, relation: .LessThanOrEqualTo)
  120. }
  121. #endif
  122. public func lessThanOrEqualTo(other: Float) -> Constraint {
  123. return constrainTo(other, relation: .LessThanOrEqualTo)
  124. }
  125. public func lessThanOrEqualTo(other: Double) -> Constraint {
  126. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  127. }
  128. public func lessThanOrEqualTo(other: CGFloat) -> Constraint {
  129. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  130. }
  131. public func lessThanOrEqualTo(other: Int) -> Constraint {
  132. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  133. }
  134. public func lessThanOrEqualTo(other: UInt) -> Constraint {
  135. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  136. }
  137. public func lessThanOrEqualTo(other: CGSize) -> Constraint {
  138. return constrainTo(other, relation: .LessThanOrEqualTo)
  139. }
  140. public func lessThanOrEqualTo(other: CGPoint) -> Constraint {
  141. return constrainTo(other, relation: .LessThanOrEqualTo)
  142. }
  143. public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
  144. return constrainTo(other, relation: .LessThanOrEqualTo)
  145. }
  146. // MARK: greaterThanOrEqualTo
  147. public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint {
  148. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  149. }
  150. func greaterThanOrEqualTo(other: View) -> Constraint {
  151. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  152. }
  153. #if os(iOS)
  154. public func greaterThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  155. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  156. }
  157. #endif
  158. public func greaterThanOrEqualTo(other: Float) -> Constraint {
  159. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  160. }
  161. public func greaterThanOrEqualTo(other: Double) -> Constraint {
  162. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  163. }
  164. public func greaterThanOrEqualTo(other: CGFloat) -> Constraint {
  165. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  166. }
  167. public func greaterThanOrEqualTo(other: Int) -> Constraint {
  168. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  169. }
  170. public func greaterThanOrEqualTo(other: UInt) -> Constraint {
  171. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  172. }
  173. public func greaterThanOrEqualTo(other: CGSize) -> Constraint {
  174. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  175. }
  176. public func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
  177. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  178. }
  179. public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
  180. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  181. }
  182. // MARK: multiplier
  183. public func multipliedBy(amount: Float) -> Constraint {
  184. self.multiplier = amount
  185. return self
  186. }
  187. public func multipliedBy(amount: Double) -> Constraint {
  188. return self.multipliedBy(Float(amount))
  189. }
  190. public func multipliedBy(amount: CGFloat) -> Constraint {
  191. return self.multipliedBy(Float(amount))
  192. }
  193. public func multipliedBy(amount: Int) -> Constraint {
  194. return self.multipliedBy(Float(amount))
  195. }
  196. public func multipliedBy(amount: UInt) -> Constraint {
  197. return self.multipliedBy(Float(amount))
  198. }
  199. public func dividedBy(amount: Float) -> Constraint {
  200. self.multiplier = 1.0 / amount;
  201. return self
  202. }
  203. public func dividedBy(amount: Double) -> Constraint {
  204. return self.dividedBy(Float(amount))
  205. }
  206. public func dividedBy(amount: CGFloat) -> Constraint {
  207. return self.dividedBy(Float(amount))
  208. }
  209. public func dividedBy(amount: Int) -> Constraint {
  210. return self.dividedBy(Float(amount))
  211. }
  212. public func dividedBy(amount: UInt) -> Constraint {
  213. return self.dividedBy(Float(amount))
  214. }
  215. // MARK: priority
  216. public func priority(priority: Float) -> Constraint {
  217. self.priority = priority
  218. return self
  219. }
  220. public func priority(priority: Double) -> Constraint {
  221. return self.priority(Float(priority))
  222. }
  223. public func priority(priority: CGFloat) -> Constraint {
  224. return self.priority(Float(priority))
  225. }
  226. public func priority(priority: UInt) -> Constraint {
  227. return self.priority(Float(priority))
  228. }
  229. public func priority(priority: Int) -> Constraint {
  230. return self.priority(Float(priority))
  231. }
  232. public func priorityRequired() -> Constraint {
  233. return self.priority(1000.0)
  234. }
  235. public func priorityHigh() -> Constraint {
  236. return self.priority(750.0)
  237. }
  238. public func priorityLow() -> Constraint {
  239. return self.priority(250.0)
  240. }
  241. // MARK: offset
  242. public func offset(amount: Float) -> Constraint {
  243. self.offset = amount
  244. return self
  245. }
  246. public func offset(amount: Double) -> Constraint {
  247. return self.offset(Float(amount))
  248. }
  249. public func offset(amount: CGFloat) -> Constraint {
  250. return self.offset(Float(amount))
  251. }
  252. public func offset(amount: Int) -> Constraint {
  253. return self.offset(Float(amount))
  254. }
  255. public func offset(amount: UInt) -> Constraint {
  256. return self.offset(Float(amount))
  257. }
  258. public func offset(amount: CGPoint) -> Constraint {
  259. self.offset = amount
  260. return self
  261. }
  262. public func offset(amount: CGSize) -> Constraint {
  263. self.offset = amount
  264. return self
  265. }
  266. public func offset(amount: EdgeInsets) -> Constraint {
  267. self.offset = amount
  268. return self
  269. }
  270. // MARK: insets
  271. public func insets(amount: EdgeInsets) -> Constraint {
  272. self.offset = amount
  273. return self
  274. }
  275. // MARK: internal
  276. internal func install(updateExisting: Bool = false) -> Array<LayoutConstraint> {
  277. if self.installedOnView != nil {
  278. NSException(name: "Cannot Install Constraint", reason: "Already installed", userInfo: nil).raise()
  279. return []
  280. }
  281. var installOnView: View? = nil
  282. if self.toItem.view != nil {
  283. installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
  284. if installOnView == nil {
  285. NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
  286. return []
  287. }
  288. } else {
  289. installOnView = self.fromItem.view?.superview
  290. if installOnView == nil {
  291. NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
  292. return []
  293. }
  294. }
  295. var layoutConstraints: Array<LayoutConstraint> = []
  296. let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
  297. let layoutToAttributes = self.toItem.attributes.layoutAttributes
  298. // get layout from
  299. let layoutFrom: View? = self.fromItem.view
  300. // get layout relation
  301. let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
  302. for layoutFromAttribute in layoutFromAttributes {
  303. // get layout to attribute
  304. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  305. // get layout constant
  306. var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
  307. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  308. // get layout to
  309. var layoutTo: View? = self.toItem.view
  310. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  311. layoutTo = installOnView
  312. }
  313. // create layout constraint
  314. let layoutConstraint = LayoutConstraint(
  315. item: layoutFrom!,
  316. attribute: layoutFromAttribute,
  317. relatedBy: layoutRelation,
  318. toItem: layoutTo,
  319. attribute: layoutToAttribute,
  320. multiplier: CGFloat(self.multiplier),
  321. constant: layoutConstant)
  322. // set priority
  323. layoutConstraint.priority = self.priority
  324. // set constraint
  325. layoutConstraint.constraint = self
  326. layoutConstraints.append(layoutConstraint)
  327. }
  328. // special logic for updating
  329. if updateExisting {
  330. // get existing constraints for this view
  331. let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
  332. // array that will contain only new layout constraints
  333. var newLayoutConstraints = Array<LayoutConstraint>()
  334. // begin looping
  335. for layoutConstraint in layoutConstraints {
  336. // layout constraint that should be updated
  337. var updateLayoutConstraint: LayoutConstraint? = nil
  338. // loop through existing and check for match
  339. for existingLayoutConstraint in existingLayoutConstraints {
  340. if existingLayoutConstraint == layoutConstraint {
  341. updateLayoutConstraint = existingLayoutConstraint
  342. break
  343. }
  344. }
  345. // if we have existing one lets just update the constant
  346. if updateLayoutConstraint != nil {
  347. updateLayoutConstraint!.constant = layoutConstraint.constant
  348. }
  349. // otherwise add this layout constraint to new list
  350. else {
  351. newLayoutConstraints.append(layoutConstraint)
  352. }
  353. }
  354. // set constraints to only new ones
  355. layoutConstraints = newLayoutConstraints
  356. }
  357. // add constarints
  358. installOnView!.addConstraints(layoutConstraints)
  359. self.installedOnView = installOnView
  360. return layoutConstraints
  361. }
  362. internal func uninstall() {
  363. if let view = self.installedOnView {
  364. #if os(iOS)
  365. var installedConstraints = view.constraints()
  366. #else
  367. var installedConstraints = view.constraints
  368. #endif
  369. var constraintsToRemove: Array<LayoutConstraint> = []
  370. for installedConstraint in installedConstraints {
  371. if let layoutConstraint = installedConstraint as? LayoutConstraint {
  372. if layoutConstraint.constraint === self {
  373. constraintsToRemove.append(layoutConstraint)
  374. }
  375. }
  376. }
  377. if constraintsToRemove.count > 0 {
  378. view.removeConstraints(constraintsToRemove)
  379. }
  380. }
  381. self.installedOnView = nil
  382. }
  383. // MARK: private
  384. private let fromItem: ConstraintItem
  385. private var toItem: ConstraintItem
  386. private var relation: ConstraintRelation?
  387. private var constant: Any?
  388. private var multiplier: Float = 1.0
  389. private var priority: Float = 1000.0
  390. private var offset: Any?
  391. private weak var installedOnView: View?
  392. private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
  393. if self.relation == nil {
  394. self.fromItem.attributes += attributes
  395. }
  396. return self
  397. }
  398. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
  399. if other.attributes != ConstraintAttributes.None {
  400. var toLayoutAttributes = other.attributes.layoutAttributes
  401. if toLayoutAttributes.count > 1 {
  402. var fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  403. if toLayoutAttributes != fromLayoutAttributes {
  404. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  405. return self
  406. }
  407. other.attributes = ConstraintAttributes.None
  408. }
  409. }
  410. self.toItem = other
  411. self.relation = relation
  412. return self
  413. }
  414. private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
  415. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  416. }
  417. #if os(iOS)
  418. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> Constraint {
  419. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  420. }
  421. #endif
  422. private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
  423. self.constant = other
  424. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  425. }
  426. private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
  427. self.constant = other
  428. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  429. }
  430. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
  431. self.constant = other
  432. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  433. }
  434. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
  435. self.constant = other
  436. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  437. }
  438. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
  439. self.constant = other
  440. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  441. }
  442. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  443. var closestCommonSuperview: View?
  444. var secondViewSuperview: View? = toView
  445. while closestCommonSuperview == nil && secondViewSuperview != nil {
  446. var firstViewSuperview = fromView
  447. while closestCommonSuperview == nil && firstViewSuperview != nil {
  448. if secondViewSuperview == firstViewSuperview {
  449. closestCommonSuperview = secondViewSuperview
  450. }
  451. firstViewSuperview = firstViewSuperview?.superview
  452. }
  453. secondViewSuperview = secondViewSuperview?.superview
  454. }
  455. return closestCommonSuperview
  456. }
  457. }
  458. private extension NSLayoutAttribute {
  459. private func snp_offsetForValue(value: Any?) -> CGFloat {
  460. // Float
  461. if let float = value as? Float {
  462. return CGFloat(float)
  463. }
  464. // Double
  465. else if let double = value as? Double {
  466. return CGFloat(double)
  467. }
  468. // UInt
  469. else if let int = value as? Int {
  470. return CGFloat(int)
  471. }
  472. // Int
  473. else if let uint = value as? UInt {
  474. return CGFloat(uint)
  475. }
  476. // CGFloat
  477. else if let float = value as? CGFloat {
  478. return float
  479. }
  480. // CGSize
  481. else if let size = value as? CGSize {
  482. if self == .Width {
  483. return size.width
  484. } else if self == .Height {
  485. return size.height
  486. }
  487. }
  488. // CGPoint
  489. else if let point = value as? CGPoint {
  490. if self == .Left || self == .CenterX {
  491. return point.x
  492. } else if self == .Top || self == .CenterY {
  493. return point.y
  494. } else if self == .Right {
  495. return -point.x
  496. } else if self == .Bottom {
  497. return -point.y
  498. }
  499. }
  500. // EdgeInsets
  501. else if let insets = value as? EdgeInsets {
  502. if self == .Left {
  503. return insets.left
  504. } else if self == .Top {
  505. return insets.top
  506. } else if self == .Right {
  507. return -insets.right
  508. } else if self == .Bottom {
  509. return -insets.bottom
  510. }
  511. }
  512. return CGFloat(0)
  513. }
  514. private func snp_constantForValue(value: Any?) -> CGFloat {
  515. // Float
  516. if let float = value as? Float {
  517. return CGFloat(float)
  518. }
  519. // Double
  520. else if let double = value as? Double {
  521. return CGFloat(double)
  522. }
  523. // UInt
  524. else if let int = value as? Int {
  525. return CGFloat(int)
  526. }
  527. // Int
  528. else if let uint = value as? UInt {
  529. return CGFloat(uint)
  530. }
  531. // CGFloat
  532. else if let float = value as? CGFloat {
  533. return float
  534. }
  535. // CGSize
  536. else if let size = value as? CGSize {
  537. if self == .Width {
  538. return size.width
  539. } else if self == .Height {
  540. return size.height
  541. }
  542. }
  543. // CGPoint
  544. else if let point = value as? CGPoint {
  545. if self == .Left || self == .CenterX {
  546. return point.x
  547. } else if self == .Top || self == .CenterY {
  548. return point.y
  549. } else if self == .Right {
  550. return point.x
  551. } else if self == .Bottom {
  552. return point.y
  553. }
  554. }
  555. // EdgeInsets
  556. else if let insets = value as? EdgeInsets {
  557. if self == .Left {
  558. return insets.left
  559. } else if self == .Top {
  560. return insets.top
  561. } else if self == .Right {
  562. return -insets.right
  563. } else if self == .Bottom {
  564. return -insets.bottom
  565. }
  566. }
  567. return CGFloat(0);
  568. }
  569. }
  570. internal func ==(left: Constraint, right: Constraint) -> Bool {
  571. return (left.fromItem == right.fromItem &&
  572. left.toItem == right.toItem &&
  573. left.relation == right.relation &&
  574. left.multiplier == right.multiplier &&
  575. left.priority == right.priority)
  576. }