ConstraintBuilder.swift 33 KB

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