ConstraintBuilder.swift 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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.offset = 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.offset = amount
  372. return self
  373. }
  374. func offset(amount: CGSize) -> ConstraintBuilderMultipliable {
  375. self.offset = amount
  376. return self
  377. }
  378. func offset(amount: EdgeInsets) -> ConstraintBuilderMultipliable {
  379. self.offset = amount
  380. return self
  381. }
  382. // MARK: insets
  383. func insets(amount: EdgeInsets) -> ConstraintBuilderMultipliable {
  384. self.offset = amount
  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. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  458. // get layout to
  459. var layoutTo: View? = self.toItem.view
  460. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  461. layoutTo = installOnView
  462. }
  463. // create layout constraint
  464. let layoutConstraint = LayoutConstraint(
  465. item: layoutFrom!,
  466. attribute: layoutFromAttribute,
  467. relatedBy: layoutRelation,
  468. toItem: layoutTo,
  469. attribute: layoutToAttribute,
  470. multiplier: CGFloat(self.multiplier),
  471. constant: layoutConstant)
  472. // set priority
  473. layoutConstraint.priority = self.priority
  474. // set constraint
  475. layoutConstraint.snp_constraint = self
  476. newLayoutConstraints.append(layoutConstraint)
  477. }
  478. // special logic for updating
  479. if updateExisting {
  480. // get existing constraints for this view
  481. let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
  482. // array that will contain only new layout constraints to keep
  483. var newLayoutConstraintsToKeep = [LayoutConstraint]()
  484. // begin looping
  485. for layoutConstraint in newLayoutConstraints {
  486. // layout constraint that should be updated
  487. var updateLayoutConstraint: LayoutConstraint? = nil
  488. // loop through existing and check for match
  489. for existingLayoutConstraint in existingLayoutConstraints {
  490. if existingLayoutConstraint == layoutConstraint {
  491. updateLayoutConstraint = existingLayoutConstraint
  492. break
  493. }
  494. }
  495. // if we have existing one lets just update the constant
  496. if updateLayoutConstraint != nil {
  497. updateLayoutConstraint!.constant = layoutConstraint.constant
  498. }
  499. // otherwise add this layout constraint to new keep list
  500. else {
  501. newLayoutConstraintsToKeep.append(layoutConstraint)
  502. }
  503. }
  504. // set constraints to only new ones
  505. newLayoutConstraints = newLayoutConstraintsToKeep
  506. }
  507. // add constraints
  508. installOnView!.addConstraints(newLayoutConstraints)
  509. // set install info
  510. self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable())
  511. // store which layout constraints are installed for this constraint
  512. for layoutConstraint in newLayoutConstraints {
  513. self.installInfo!.layoutConstraints.addObject(layoutConstraint)
  514. }
  515. // store the layout constraints against the layout from view
  516. layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints
  517. // return the new constraints
  518. return newLayoutConstraints
  519. }
  520. func uninstallFromView() {
  521. if let installInfo = self.installInfo,
  522. let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] {
  523. if installedLayoutConstraints.count > 0 {
  524. if let installedOnView = installInfo.view {
  525. // remove the constraints from the UIView's storage
  526. installedOnView.removeConstraints(installedLayoutConstraints)
  527. }
  528. // remove the constraints from the from item view
  529. if let fromView = self.fromItem.view {
  530. fromView.snp_installedLayoutConstraints = fromView.snp_installedLayoutConstraints.filter {
  531. return !contains(installedLayoutConstraints, $0)
  532. }
  533. }
  534. }
  535. }
  536. self.installInfo = nil
  537. }
  538. // MARK: private
  539. private let fromItem: ConstraintItem
  540. private var toItem: ConstraintItem
  541. private var relation: ConstraintRelation?
  542. private var constant: Any = Float(0.0)
  543. private var multiplier: Float = 1.0
  544. private var priority: Float = 1000.0
  545. private var offset: Any = Float(0.0)
  546. private var installInfo: ConstraintInstallInfo?
  547. private func addConstraint(attributes: ConstraintAttributes) -> ConstraintBuilder {
  548. if self.relation == nil {
  549. self.fromItem.attributes += attributes
  550. }
  551. return self
  552. }
  553. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> ConstraintBuilder {
  554. if other.attributes != ConstraintAttributes.None {
  555. let toLayoutAttributes = other.attributes.layoutAttributes
  556. if toLayoutAttributes.count > 1 {
  557. let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  558. if toLayoutAttributes != fromLayoutAttributes {
  559. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  560. return self
  561. }
  562. other.attributes = ConstraintAttributes.None
  563. }
  564. }
  565. self.toItem = other
  566. self.relation = relation
  567. return self
  568. }
  569. private func constrainTo(other: View, relation: ConstraintRelation) -> ConstraintBuilder {
  570. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  571. }
  572. #if os(iOS)
  573. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> ConstraintBuilder {
  574. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  575. }
  576. #endif
  577. private func constrainTo(other: Float, relation: ConstraintRelation) -> ConstraintBuilder {
  578. self.constant = other
  579. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  580. }
  581. private func constrainTo(other: Double, relation: ConstraintRelation) -> ConstraintBuilder {
  582. self.constant = other
  583. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  584. }
  585. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> ConstraintBuilder {
  586. self.constant = other
  587. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  588. }
  589. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> ConstraintBuilder {
  590. self.constant = other
  591. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  592. }
  593. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> ConstraintBuilder {
  594. self.constant = other
  595. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  596. }
  597. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  598. var views = Set<View>()
  599. var fromView = fromView
  600. var toView = toView
  601. do {
  602. if let view = toView {
  603. if views.contains(view) {
  604. return view
  605. }
  606. views.insert(view)
  607. toView = view.superview
  608. }
  609. if let view = fromView {
  610. if views.contains(view) {
  611. return view
  612. }
  613. views.insert(view)
  614. fromView = view.superview
  615. }
  616. } while (fromView != nil || toView != nil)
  617. return nil
  618. }
  619. }
  620. private extension NSLayoutAttribute {
  621. private func snp_offsetForValue(value: Any?) -> CGFloat {
  622. // Float
  623. if let float = value as? Float {
  624. return CGFloat(float)
  625. }
  626. // Double
  627. else if let double = value as? Double {
  628. return CGFloat(double)
  629. }
  630. // UInt
  631. else if let int = value as? Int {
  632. return CGFloat(int)
  633. }
  634. // Int
  635. else if let uint = value as? UInt {
  636. return CGFloat(uint)
  637. }
  638. // CGFloat
  639. else if let float = value as? CGFloat {
  640. return float
  641. }
  642. // CGSize
  643. else if let size = value as? CGSize {
  644. if self == .Width {
  645. return size.width
  646. } else if self == .Height {
  647. return size.height
  648. }
  649. }
  650. // CGPoint
  651. else if let point = value as? CGPoint {
  652. #if os(iOS)
  653. switch self {
  654. case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x
  655. case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y
  656. case .Right, .RightMargin: return -point.x
  657. case .Bottom, .BottomMargin: return -point.y
  658. case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
  659. case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
  660. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  661. }
  662. #else
  663. switch self {
  664. case .Left, .CenterX: return point.x
  665. case .Top, .CenterY, .Baseline: return point.y
  666. case .Right: return -point.x
  667. case .Bottom: return -point.y
  668. case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
  669. case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
  670. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  671. }
  672. #endif
  673. }
  674. // EdgeInsets
  675. else if let insets = value as? EdgeInsets {
  676. #if os(iOS)
  677. switch self {
  678. case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left
  679. case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top
  680. case .Right, .RightMargin: return -insets.right
  681. case .Bottom, .BottomMargin: return -insets.bottom
  682. case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
  683. case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
  684. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  685. }
  686. #else
  687. switch self {
  688. case .Left, .CenterX: return insets.left
  689. case .Top, .CenterY, .Baseline: return insets.top
  690. case .Right: return -insets.right
  691. case .Bottom: return -insets.bottom
  692. case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
  693. case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
  694. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  695. }
  696. #endif
  697. }
  698. return CGFloat(0)
  699. }
  700. private func snp_constantForValue(value: Any?) -> CGFloat {
  701. // Float
  702. if let float = value as? Float {
  703. return CGFloat(float)
  704. }
  705. // Double
  706. else if let double = value as? Double {
  707. return CGFloat(double)
  708. }
  709. // UInt
  710. else if let int = value as? Int {
  711. return CGFloat(int)
  712. }
  713. // Int
  714. else if let uint = value as? UInt {
  715. return CGFloat(uint)
  716. }
  717. // CGFloat
  718. else if let float = value as? CGFloat {
  719. return float
  720. }
  721. // CGSize
  722. else if let size = value as? CGSize {
  723. if self == .Width {
  724. return size.width
  725. } else if self == .Height {
  726. return size.height
  727. }
  728. }
  729. // CGPoint
  730. else if let point = value as? CGPoint {
  731. #if os(iOS)
  732. switch self {
  733. case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x
  734. case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y
  735. case .Right, .RightMargin: return point.x
  736. case .Bottom, .BottomMargin: return point.y
  737. case .Leading, .LeadingMargin: return point.x
  738. case .Trailing, .TrailingMargin: return point.x
  739. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  740. }
  741. #else
  742. switch self {
  743. case .Left, .CenterX: return point.x
  744. case .Top, .CenterY, .Baseline: return point.y
  745. case .Right: return point.x
  746. case .Bottom: return point.y
  747. case .Leading: return point.x
  748. case .Trailing: return point.x
  749. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  750. }
  751. #endif
  752. }
  753. // EdgeInsets
  754. else if let insets = value as? EdgeInsets {
  755. #if os(iOS)
  756. switch self {
  757. case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left
  758. case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top
  759. case .Right, .RightMargin: return insets.right
  760. case .Bottom, .BottomMargin: return insets.bottom
  761. case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right
  762. case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left
  763. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  764. }
  765. #else
  766. switch self {
  767. case .Left, .CenterX: return insets.left
  768. case .Top, .CenterY, .Baseline: return insets.top
  769. case .Right: return insets.right
  770. case .Bottom: return insets.bottom
  771. case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right
  772. case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left
  773. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  774. }
  775. #endif
  776. }
  777. return CGFloat(0);
  778. }
  779. }
  780. private struct ConstraintInstallInfo {
  781. weak var view: View? = nil
  782. let layoutConstraints: NSHashTable
  783. }
  784. internal func ==(left: ConstraintBuilder, right: ConstraintBuilder) -> Bool {
  785. return (left.fromItem == right.fromItem &&
  786. left.toItem == right.toItem &&
  787. left.relation == right.relation &&
  788. left.multiplier == right.multiplier &&
  789. left.priority == right.priority)
  790. }