Constraint.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. /**
  29. * Constraint is a single item that defines all the properties for a single ConstraintMaker chain
  30. */
  31. final public class Constraint {
  32. public var left: Constraint { return self.addConstraint(ConstraintAttributes.Left) }
  33. public var top: Constraint { return self.addConstraint(ConstraintAttributes.Top) }
  34. public var right: Constraint { return self.addConstraint(ConstraintAttributes.Right) }
  35. public var bottom: Constraint { return self.addConstraint(ConstraintAttributes.Bottom) }
  36. public var leading: Constraint { return self.addConstraint(ConstraintAttributes.Leading) }
  37. public var trailing: Constraint { return self.addConstraint(ConstraintAttributes.Trailing) }
  38. public var width: Constraint { return self.addConstraint(ConstraintAttributes.Width) }
  39. public var height: Constraint { return self.addConstraint(ConstraintAttributes.Height) }
  40. public var centerX: Constraint { return self.addConstraint(ConstraintAttributes.CenterX) }
  41. public var centerY: Constraint { return self.addConstraint(ConstraintAttributes.CenterY) }
  42. public var baseline: Constraint { return self.addConstraint(ConstraintAttributes.Baseline) }
  43. public var and: Constraint {
  44. if self.relation != nil {
  45. fatalError("And is semantic only and can only be used before a relation is set.")
  46. }
  47. return self
  48. }
  49. public var with: Constraint { return self }
  50. // MARK: initializer
  51. internal init(fromItem: ConstraintItem) {
  52. self.fromItem = fromItem
  53. self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
  54. }
  55. // MARK: equalTo
  56. public func equalTo(other: ConstraintItem) -> Constraint {
  57. return self.constrainTo(other, relation: .Equal)
  58. }
  59. public func equalTo(other: View) -> Constraint {
  60. return self.constrainTo(other, relation: .Equal)
  61. }
  62. #if os(iOS)
  63. public func equalTo(other: UILayoutSupport) -> Constraint {
  64. return self.constrainTo(other, relation: .Equal)
  65. }
  66. #endif
  67. public func equalTo(other: Float) -> Constraint {
  68. return self.constrainTo(other, relation: .Equal)
  69. }
  70. public func equalTo(other: Double) -> Constraint {
  71. return self.constrainTo(Float(other), relation: .Equal)
  72. }
  73. public func equalTo(other: CGFloat) -> Constraint {
  74. return self.constrainTo(Float(other), relation: .Equal)
  75. }
  76. public func equalTo(other: Int) -> Constraint {
  77. return self.constrainTo(Float(other), relation: .Equal)
  78. }
  79. public func equalTo(other: UInt) -> Constraint {
  80. return self.constrainTo(Float(other), relation: .Equal)
  81. }
  82. public func equalTo(other: CGSize) -> Constraint {
  83. return self.constrainTo(other, relation: .Equal)
  84. }
  85. public func equalTo(other: CGPoint) -> Constraint {
  86. return self.constrainTo(other, relation: .Equal)
  87. }
  88. public func equalTo(other: EdgeInsets) -> Constraint {
  89. return self.constrainTo(other, relation: .Equal)
  90. }
  91. // MARK: lessThanOrEqualTo
  92. public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint {
  93. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  94. }
  95. public func lessThanOrEqualTo(other: View) -> Constraint {
  96. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  97. }
  98. #if os(iOS)
  99. public func lessThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  100. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  101. }
  102. #endif
  103. public func lessThanOrEqualTo(other: Float) -> Constraint {
  104. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  105. }
  106. public func lessThanOrEqualTo(other: Double) -> Constraint {
  107. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  108. }
  109. public func lessThanOrEqualTo(other: CGFloat) -> Constraint {
  110. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  111. }
  112. public func lessThanOrEqualTo(other: Int) -> Constraint {
  113. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  114. }
  115. public func lessThanOrEqualTo(other: UInt) -> Constraint {
  116. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  117. }
  118. public func lessThanOrEqualTo(other: CGSize) -> Constraint {
  119. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  120. }
  121. public func lessThanOrEqualTo(other: CGPoint) -> Constraint {
  122. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  123. }
  124. public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
  125. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  126. }
  127. // MARK: greaterThanOrEqualTo
  128. public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint {
  129. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  130. }
  131. public func greaterThanOrEqualTo(other: View) -> Constraint {
  132. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  133. }
  134. #if os(iOS)
  135. public func greaterThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  136. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  137. }
  138. #endif
  139. public func greaterThanOrEqualTo(other: Float) -> Constraint {
  140. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  141. }
  142. public func greaterThanOrEqualTo(other: Double) -> Constraint {
  143. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  144. }
  145. public func greaterThanOrEqualTo(other: CGFloat) -> Constraint {
  146. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  147. }
  148. public func greaterThanOrEqualTo(other: Int) -> Constraint {
  149. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  150. }
  151. public func greaterThanOrEqualTo(other: UInt) -> Constraint {
  152. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  153. }
  154. public func greaterThanOrEqualTo(other: CGSize) -> Constraint {
  155. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  156. }
  157. public func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
  158. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  159. }
  160. public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
  161. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  162. }
  163. // MARK: multiplier
  164. public func multipliedBy(amount: Float) -> Constraint {
  165. self.multiplier = amount
  166. return self
  167. }
  168. public func multipliedBy(amount: Double) -> Constraint {
  169. return self.multipliedBy(Float(amount))
  170. }
  171. public func multipliedBy(amount: CGFloat) -> Constraint {
  172. return self.multipliedBy(Float(amount))
  173. }
  174. public func multipliedBy(amount: Int) -> Constraint {
  175. return self.multipliedBy(Float(amount))
  176. }
  177. public func multipliedBy(amount: UInt) -> Constraint {
  178. return self.multipliedBy(Float(amount))
  179. }
  180. public func dividedBy(amount: Float) -> Constraint {
  181. self.multiplier = 1.0 / amount;
  182. return self
  183. }
  184. public func dividedBy(amount: Double) -> Constraint {
  185. return self.dividedBy(Float(amount))
  186. }
  187. public func dividedBy(amount: CGFloat) -> Constraint {
  188. return self.dividedBy(Float(amount))
  189. }
  190. public func dividedBy(amount: Int) -> Constraint {
  191. return self.dividedBy(Float(amount))
  192. }
  193. public func dividedBy(amount: UInt) -> Constraint {
  194. return self.dividedBy(Float(amount))
  195. }
  196. // MARK: priority
  197. public func priority(priority: Float) -> Constraint {
  198. self.priority = priority
  199. return self
  200. }
  201. public func priority(priority: Double) -> Constraint {
  202. return self.priority(Float(priority))
  203. }
  204. public func priority(priority: CGFloat) -> Constraint {
  205. return self.priority(Float(priority))
  206. }
  207. public func priority(priority: UInt) -> Constraint {
  208. return self.priority(Float(priority))
  209. }
  210. public func priority(priority: Int) -> Constraint {
  211. return self.priority(Float(priority))
  212. }
  213. public func priorityRequired() -> Constraint {
  214. return self.priority(1000.0)
  215. }
  216. public func priorityHigh() -> Constraint {
  217. return self.priority(750.0)
  218. }
  219. public func priorityMedium() -> Constraint {
  220. #if os(iOS)
  221. return self.priority(500.0)
  222. #else
  223. return self.priority(501.0)
  224. #endif
  225. }
  226. public func priorityLow() -> Constraint {
  227. return self.priority(250.0)
  228. }
  229. // MARK: offset
  230. public func offset(amount: Float) -> Constraint {
  231. self.offset = amount
  232. return self
  233. }
  234. public func offset(amount: Double) -> Constraint {
  235. return self.offset(Float(amount))
  236. }
  237. public func offset(amount: CGFloat) -> Constraint {
  238. return self.offset(Float(amount))
  239. }
  240. public func offset(amount: Int) -> Constraint {
  241. return self.offset(Float(amount))
  242. }
  243. public func offset(amount: UInt) -> Constraint {
  244. return self.offset(Float(amount))
  245. }
  246. public func offset(amount: CGPoint) -> Constraint {
  247. self.offset = amount
  248. return self
  249. }
  250. public func offset(amount: CGSize) -> Constraint {
  251. self.offset = amount
  252. return self
  253. }
  254. public func offset(amount: EdgeInsets) -> Constraint {
  255. self.offset = amount
  256. return self
  257. }
  258. // MARK: insets
  259. public func insets(amount: EdgeInsets) -> Constraint {
  260. self.offset = amount
  261. return self
  262. }
  263. // MARK: install / uninstall
  264. public func install() -> [LayoutConstraint] {
  265. return self.installOnView(updateExisting: false)
  266. }
  267. public func uninstall() {
  268. self.uninstallFromView()
  269. }
  270. // MARK: internal
  271. internal func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] {
  272. var installOnView: View? = nil
  273. if self.toItem.view != nil {
  274. installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
  275. if installOnView == nil {
  276. NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
  277. return []
  278. }
  279. } else {
  280. installOnView = self.fromItem.view?.superview
  281. if installOnView == nil {
  282. if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
  283. installOnView = self.fromItem.view
  284. }
  285. if installOnView == nil {
  286. NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
  287. return []
  288. }
  289. }
  290. }
  291. if let installedOnView = self.installInfo?.view {
  292. if installedOnView != installOnView {
  293. NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise()
  294. return []
  295. }
  296. return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? []
  297. }
  298. var newLayoutConstraints = [LayoutConstraint]()
  299. let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
  300. let layoutToAttributes = self.toItem.attributes.layoutAttributes
  301. // get layout from
  302. let layoutFrom: View? = self.fromItem.view
  303. // get layout relation
  304. let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
  305. for layoutFromAttribute in layoutFromAttributes {
  306. // get layout to attribute
  307. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  308. // get layout constant
  309. var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
  310. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  311. // get layout to
  312. var layoutTo: View? = self.toItem.view
  313. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  314. layoutTo = installOnView
  315. }
  316. // create layout constraint
  317. let layoutConstraint = LayoutConstraint(
  318. item: layoutFrom!,
  319. attribute: layoutFromAttribute,
  320. relatedBy: layoutRelation,
  321. toItem: layoutTo,
  322. attribute: layoutToAttribute,
  323. multiplier: CGFloat(self.multiplier),
  324. constant: layoutConstant)
  325. // set priority
  326. layoutConstraint.priority = self.priority
  327. // set constraint
  328. layoutConstraint.snp_constraint = self
  329. newLayoutConstraints.append(layoutConstraint)
  330. }
  331. // special logic for updating
  332. if updateExisting {
  333. // get existing constraints for this view
  334. let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
  335. // array that will contain only new layout constraints to keep
  336. var newLayoutConstraintsToKeep = [LayoutConstraint]()
  337. // begin looping
  338. for layoutConstraint in newLayoutConstraints {
  339. // layout constraint that should be updated
  340. var updateLayoutConstraint: LayoutConstraint? = nil
  341. // loop through existing and check for match
  342. for existingLayoutConstraint in existingLayoutConstraints {
  343. if existingLayoutConstraint == layoutConstraint {
  344. updateLayoutConstraint = existingLayoutConstraint
  345. break
  346. }
  347. }
  348. // if we have existing one lets just update the constant
  349. if updateLayoutConstraint != nil {
  350. updateLayoutConstraint!.constant = layoutConstraint.constant
  351. }
  352. // otherwise add this layout constraint to new keep list
  353. else {
  354. newLayoutConstraintsToKeep.append(layoutConstraint)
  355. }
  356. }
  357. // set constraints to only new ones
  358. newLayoutConstraints = newLayoutConstraintsToKeep
  359. }
  360. // add constraints
  361. installOnView!.addConstraints(newLayoutConstraints)
  362. // set install info
  363. self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable())
  364. // store which layout constraints are installed for this constraint
  365. for layoutConstraint in newLayoutConstraints {
  366. self.installInfo!.layoutConstraints.addObject(layoutConstraint)
  367. }
  368. // store the layout constraints against the layout from view
  369. layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints
  370. // return the new constraints
  371. return newLayoutConstraints
  372. }
  373. internal func uninstallFromView() {
  374. if let installInfo = self.installInfo,
  375. let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] {
  376. if installedLayoutConstraints.count > 0 {
  377. if let installedOnView = installInfo.view {
  378. // remove the constraints from the UIView's storage
  379. installedOnView.removeConstraints(installedLayoutConstraints)
  380. }
  381. // remove the constraints from the from item view
  382. if let fromView = self.fromItem.view {
  383. fromView.snp_installedLayoutConstraints = fromView.snp_installedLayoutConstraints.filter {
  384. return !contains(installedLayoutConstraints, $0)
  385. }
  386. }
  387. }
  388. }
  389. self.installInfo = nil
  390. }
  391. // MARK: private
  392. private let fromItem: ConstraintItem
  393. private var toItem: ConstraintItem
  394. private var relation: ConstraintRelation?
  395. private var constant: Any = Float(0.0)
  396. private var multiplier: Float = 1.0
  397. private var priority: Float = 1000.0
  398. private var offset: Any = Float(0.0)
  399. private var installInfo: ConstraintInstallInfo?
  400. private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
  401. if self.relation == nil {
  402. self.fromItem.attributes += attributes
  403. }
  404. return self
  405. }
  406. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
  407. if other.attributes != ConstraintAttributes.None {
  408. let toLayoutAttributes = other.attributes.layoutAttributes
  409. if toLayoutAttributes.count > 1 {
  410. let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  411. if toLayoutAttributes != fromLayoutAttributes {
  412. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  413. return self
  414. }
  415. other.attributes = ConstraintAttributes.None
  416. }
  417. }
  418. self.toItem = other
  419. self.relation = relation
  420. return self
  421. }
  422. private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
  423. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  424. }
  425. #if os(iOS)
  426. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> Constraint {
  427. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  428. }
  429. #endif
  430. private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
  431. self.constant = other
  432. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  433. }
  434. private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
  435. self.constant = other
  436. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  437. }
  438. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
  439. self.constant = other
  440. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  441. }
  442. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
  443. self.constant = other
  444. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  445. }
  446. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
  447. self.constant = other
  448. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  449. }
  450. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  451. var views = Set<View>()
  452. var fromView = fromView
  453. var toView = toView
  454. do {
  455. if let view = toView {
  456. if views.contains(view) {
  457. return view
  458. }
  459. views.insert(view)
  460. toView = view.superview
  461. }
  462. if let view = fromView {
  463. if views.contains(view) {
  464. return view
  465. }
  466. views.insert(view)
  467. fromView = view.superview
  468. }
  469. } while (fromView != nil || toView != nil)
  470. return nil
  471. }
  472. }
  473. private extension NSLayoutAttribute {
  474. private func snp_offsetForValue(value: Any?) -> CGFloat {
  475. // Float
  476. if let float = value as? Float {
  477. return CGFloat(float)
  478. }
  479. // Double
  480. else if let double = value as? Double {
  481. return CGFloat(double)
  482. }
  483. // UInt
  484. else if let int = value as? Int {
  485. return CGFloat(int)
  486. }
  487. // Int
  488. else if let uint = value as? UInt {
  489. return CGFloat(uint)
  490. }
  491. // CGFloat
  492. else if let float = value as? CGFloat {
  493. return float
  494. }
  495. // CGSize
  496. else if let size = value as? CGSize {
  497. if self == .Width {
  498. return size.width
  499. } else if self == .Height {
  500. return size.height
  501. }
  502. }
  503. // CGPoint
  504. else if let point = value as? CGPoint {
  505. if self == .Left || self == .CenterX {
  506. return point.x
  507. } else if self == .Top || self == .CenterY {
  508. return point.y
  509. } else if self == .Right {
  510. return -point.x
  511. } else if self == .Bottom {
  512. return -point.y
  513. }
  514. }
  515. // EdgeInsets
  516. else if let insets = value as? EdgeInsets {
  517. if self == .Left {
  518. return insets.left
  519. } else if self == .Top {
  520. return insets.top
  521. } else if self == .Right {
  522. return -insets.right
  523. } else if self == .Bottom {
  524. return -insets.bottom
  525. }
  526. }
  527. return CGFloat(0)
  528. }
  529. private func snp_constantForValue(value: Any?) -> CGFloat {
  530. // Float
  531. if let float = value as? Float {
  532. return CGFloat(float)
  533. }
  534. // Double
  535. else if let double = value as? Double {
  536. return CGFloat(double)
  537. }
  538. // UInt
  539. else if let int = value as? Int {
  540. return CGFloat(int)
  541. }
  542. // Int
  543. else if let uint = value as? UInt {
  544. return CGFloat(uint)
  545. }
  546. // CGFloat
  547. else if let float = value as? CGFloat {
  548. return float
  549. }
  550. // CGSize
  551. else if let size = value as? CGSize {
  552. if self == .Width {
  553. return size.width
  554. } else if self == .Height {
  555. return size.height
  556. }
  557. }
  558. // CGPoint
  559. else if let point = value as? CGPoint {
  560. if self == .Left || self == .CenterX {
  561. return point.x
  562. } else if self == .Top || self == .CenterY {
  563. return point.y
  564. } else if self == .Right {
  565. return point.x
  566. } else if self == .Bottom {
  567. return point.y
  568. }
  569. }
  570. // EdgeInsets
  571. else if let insets = value as? EdgeInsets {
  572. if self == .Left {
  573. return insets.left
  574. } else if self == .Top {
  575. return insets.top
  576. } else if self == .Right {
  577. return -insets.right
  578. } else if self == .Bottom {
  579. return -insets.bottom
  580. }
  581. }
  582. return CGFloat(0);
  583. }
  584. }
  585. private struct ConstraintInstallInfo {
  586. weak var view: UIView? = nil
  587. let layoutConstraints: NSHashTable
  588. }
  589. internal func ==(left: Constraint, right: Constraint) -> Bool {
  590. return (left.fromItem == right.fromItem &&
  591. left.toItem == right.toItem &&
  592. left.relation == right.relation &&
  593. left.multiplier == right.multiplier &&
  594. left.priority == right.priority)
  595. }