Constraint.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. //
  2. // Snappy
  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() -> 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. installOnView!.addConstraints(layoutConstraints)
  329. self.installedOnView = installOnView
  330. return layoutConstraints
  331. }
  332. internal func uninstall() {
  333. if let view = self.installedOnView {
  334. #if os(iOS)
  335. var installedConstraints = view.constraints()
  336. #else
  337. var installedConstraints = view.constraints
  338. #endif
  339. var constraintsToRemove: Array<LayoutConstraint> = []
  340. for installedConstraint in installedConstraints {
  341. if let layoutConstraint = installedConstraint as? LayoutConstraint {
  342. if layoutConstraint.constraint === self {
  343. constraintsToRemove.append(layoutConstraint)
  344. }
  345. }
  346. }
  347. if constraintsToRemove.count > 0 {
  348. view.removeConstraints(constraintsToRemove)
  349. }
  350. }
  351. self.installedOnView = nil
  352. }
  353. // MARK: private
  354. private let fromItem: ConstraintItem
  355. private var toItem: ConstraintItem
  356. private var relation: ConstraintRelation?
  357. private var constant: Any?
  358. private var multiplier: Float = 1.0
  359. private var priority: Float = 1000.0
  360. private var offset: Any?
  361. private weak var installedOnView: View?
  362. private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
  363. if self.relation == nil {
  364. self.fromItem.attributes += attributes
  365. }
  366. return self
  367. }
  368. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
  369. if other.attributes != ConstraintAttributes.None {
  370. var toLayoutAttributes = other.attributes.layoutAttributes
  371. if toLayoutAttributes.count > 1 {
  372. var fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  373. if toLayoutAttributes != fromLayoutAttributes {
  374. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  375. return self
  376. }
  377. other.attributes = ConstraintAttributes.None
  378. }
  379. }
  380. self.toItem = other
  381. self.relation = relation
  382. return self
  383. }
  384. private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
  385. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  386. }
  387. #if os(iOS)
  388. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> Constraint {
  389. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  390. }
  391. #endif
  392. private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
  393. self.constant = other
  394. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  395. }
  396. private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
  397. self.constant = other
  398. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  399. }
  400. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
  401. self.constant = other
  402. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  403. }
  404. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
  405. self.constant = other
  406. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  407. }
  408. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
  409. self.constant = other
  410. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  411. }
  412. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  413. var closestCommonSuperview: View?
  414. var secondViewSuperview: View? = toView
  415. while closestCommonSuperview == nil && secondViewSuperview != nil {
  416. var firstViewSuperview = fromView
  417. while closestCommonSuperview == nil && firstViewSuperview != nil {
  418. if secondViewSuperview == firstViewSuperview {
  419. closestCommonSuperview = secondViewSuperview
  420. }
  421. firstViewSuperview = firstViewSuperview?.superview
  422. }
  423. secondViewSuperview = secondViewSuperview?.superview
  424. }
  425. return closestCommonSuperview
  426. }
  427. }
  428. private extension NSLayoutAttribute {
  429. private func snp_offsetForValue(value: Any?) -> CGFloat {
  430. // Float
  431. if let float = value as? Float {
  432. return CGFloat(float)
  433. }
  434. // Double
  435. else if let double = value as? Double {
  436. return CGFloat(double)
  437. }
  438. // UInt
  439. else if let int = value as? Int {
  440. return CGFloat(int)
  441. }
  442. // Int
  443. else if let uint = value as? UInt {
  444. return CGFloat(uint)
  445. }
  446. // CGFloat
  447. else if let float = value as? CGFloat {
  448. return float
  449. }
  450. // CGSize
  451. else if let size = value as? CGSize {
  452. if self == .Width {
  453. return size.width
  454. } else if self == .Height {
  455. return size.height
  456. }
  457. }
  458. // CGPoint
  459. else if let point = value as? CGPoint {
  460. if self == .Left || self == .CenterX {
  461. return point.x
  462. } else if self == .Top || self == .CenterY {
  463. return point.y
  464. } else if self == .Right {
  465. return -point.x
  466. } else if self == .Bottom {
  467. return -point.y
  468. }
  469. }
  470. // EdgeInsets
  471. else if let insets = value as? EdgeInsets {
  472. if self == .Left {
  473. return insets.left
  474. } else if self == .Top {
  475. return insets.top
  476. } else if self == .Right {
  477. return -insets.right
  478. } else if self == .Bottom {
  479. return -insets.bottom
  480. }
  481. }
  482. return CGFloat(0)
  483. }
  484. private func snp_constantForValue(value: Any?) -> CGFloat {
  485. // Float
  486. if let float = value as? Float {
  487. return CGFloat(float)
  488. }
  489. // Double
  490. else if let double = value as? Double {
  491. return CGFloat(double)
  492. }
  493. // UInt
  494. else if let int = value as? Int {
  495. return CGFloat(int)
  496. }
  497. // Int
  498. else if let uint = value as? UInt {
  499. return CGFloat(uint)
  500. }
  501. // CGFloat
  502. else if let float = value as? CGFloat {
  503. return float
  504. }
  505. // CGSize
  506. else if let size = value as? CGSize {
  507. if self == .Width {
  508. return size.width
  509. } else if self == .Height {
  510. return size.height
  511. }
  512. }
  513. // CGPoint
  514. else if let point = value as? CGPoint {
  515. if self == .Left || self == .CenterX {
  516. return point.x
  517. } else if self == .Top || self == .CenterY {
  518. return point.y
  519. } else if self == .Right {
  520. return point.x
  521. } else if self == .Bottom {
  522. return point.y
  523. }
  524. }
  525. // EdgeInsets
  526. else if let insets = value as? EdgeInsets {
  527. if self == .Left {
  528. return insets.left
  529. } else if self == .Top {
  530. return insets.top
  531. } else if self == .Right {
  532. return -insets.right
  533. } else if self == .Bottom {
  534. return -insets.bottom
  535. }
  536. }
  537. return CGFloat(0);
  538. }
  539. }
  540. internal func ==(left: Constraint, right: Constraint) -> Bool {
  541. return (left.fromItem == right.fromItem &&
  542. left.toItem == right.toItem &&
  543. left.relation == right.relation &&
  544. left.multiplier == right.multiplier &&
  545. left.priority == right.priority)
  546. }