Constraint.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. //
  2. // Snap
  3. //
  4. // Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #if os(iOS)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. /**
  29. * Constraint is a protocol that exposes the public methods on a Constraint
  30. */
  31. public protocol Constraint: class {
  32. func install() -> [LayoutConstraint]
  33. func uninstall() -> Void
  34. func activate() -> Void
  35. func deactivate() -> Void
  36. }
  37. public protocol ConstraintFinalizable: class {
  38. var constraint: Constraint { get }
  39. }
  40. /**
  41. * ConstraintPriortizable is a protocol that allows a constraint to be prioritized
  42. */
  43. public protocol ConstraintPriortizable: ConstraintFinalizable {
  44. func priority(priority: Float) -> ConstraintFinalizable
  45. func priority(priority: Double) -> ConstraintFinalizable
  46. func priority(priority: CGFloat) -> ConstraintFinalizable
  47. func priority(priority: UInt) -> ConstraintFinalizable
  48. func priority(priority: Int) -> ConstraintFinalizable
  49. func priorityRequired() -> ConstraintFinalizable
  50. func priorityHigh() -> ConstraintFinalizable
  51. func priorityMedium() -> ConstraintFinalizable
  52. func priorityLow() -> ConstraintFinalizable
  53. }
  54. /**
  55. * ConstraintMultipliable is a protocol that allows a constraint to be multiplied
  56. */
  57. public protocol ConstraintMultipliable: ConstraintPriortizable {
  58. func multipliedBy(amount: Float) -> ConstraintPriortizable
  59. func multipliedBy(amount: Double) -> ConstraintPriortizable
  60. func multipliedBy(amount: CGFloat) -> ConstraintPriortizable
  61. func multipliedBy(amount: Int) -> ConstraintPriortizable
  62. func multipliedBy(amount: UInt) -> ConstraintPriortizable
  63. func dividedBy(amount: Float) -> ConstraintPriortizable
  64. func dividedBy(amount: Double) -> ConstraintPriortizable
  65. func dividedBy(amount: CGFloat) -> ConstraintPriortizable
  66. func dividedBy(amount: Int) -> ConstraintPriortizable
  67. func dividedBy(amount: UInt) -> ConstraintPriortizable
  68. }
  69. /**
  70. * ConstraintOffsetable is a protocol that allows a constraint to be offset
  71. */
  72. public protocol ConstraintOffsetable: ConstraintMultipliable {
  73. func offset(amount: Float) -> ConstraintMultipliable
  74. func offset(amount: Double) -> ConstraintMultipliable
  75. func offset(amount: CGFloat) -> ConstraintMultipliable
  76. func offset(amount: Int) -> ConstraintMultipliable
  77. func offset(amount: UInt) -> ConstraintMultipliable
  78. func offset(amount: CGPoint) -> ConstraintMultipliable
  79. func offset(amount: CGSize) -> ConstraintMultipliable
  80. func offset(amount: EdgeInsets) -> ConstraintMultipliable
  81. func insets(amount: EdgeInsets) -> ConstraintMultipliable
  82. }
  83. /**
  84. * ConstraintRelatable is a protocol that allows a constraint to be set related to another item/constant by equalTo, lessThanOrEqualTo or greaterThanOrEqualTo
  85. */
  86. public protocol ConstraintRelatable: class {
  87. func equalTo(other: ConstraintItem) -> ConstraintOffsetable
  88. func equalTo(other: View) -> ConstraintOffsetable
  89. #if os(iOS)
  90. func equalTo(other: UILayoutSupport) -> ConstraintOffsetable
  91. #endif
  92. func equalTo(other: Float) -> ConstraintMultipliable
  93. func equalTo(other: Double) -> ConstraintMultipliable
  94. func equalTo(other: CGFloat) -> ConstraintMultipliable
  95. func equalTo(other: Int) -> ConstraintMultipliable
  96. func equalTo(other: UInt) -> ConstraintMultipliable
  97. func equalTo(other: CGSize) -> ConstraintMultipliable
  98. func equalTo(other: CGPoint) -> ConstraintMultipliable
  99. func equalTo(other: EdgeInsets) -> ConstraintMultipliable
  100. func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable
  101. func lessThanOrEqualTo(other: View) -> ConstraintOffsetable
  102. #if os(iOS)
  103. func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable
  104. #endif
  105. func lessThanOrEqualTo(other: Float) -> ConstraintMultipliable
  106. func lessThanOrEqualTo(other: Double) -> ConstraintMultipliable
  107. func lessThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable
  108. func lessThanOrEqualTo(other: Int) -> ConstraintMultipliable
  109. func lessThanOrEqualTo(other: UInt) -> ConstraintMultipliable
  110. func lessThanOrEqualTo(other: CGSize) -> ConstraintMultipliable
  111. func lessThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable
  112. func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable
  113. func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable
  114. func greaterThanOrEqualTo(other: View) -> ConstraintOffsetable
  115. #if os(iOS)
  116. func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable
  117. #endif
  118. func greaterThanOrEqualTo(other: Float) -> ConstraintMultipliable
  119. func greaterThanOrEqualTo(other: Double) -> ConstraintMultipliable
  120. func greaterThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable
  121. func greaterThanOrEqualTo(other: Int) -> ConstraintMultipliable
  122. func greaterThanOrEqualTo(other: UInt) -> ConstraintMultipliable
  123. func greaterThanOrEqualTo(other: CGSize) -> ConstraintMultipliable
  124. func greaterThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable
  125. func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable
  126. }
  127. /**
  128. * ConstraintExtendable is a protocol that allows a constraint to be extended
  129. */
  130. public protocol ConstraintExtendable: ConstraintRelatable {
  131. var left: ConstraintExtendable { get }
  132. var top: ConstraintExtendable { get }
  133. var bottom: ConstraintExtendable { get }
  134. var leading: ConstraintExtendable { get }
  135. var trailing: ConstraintExtendable { get }
  136. var width: ConstraintExtendable { get }
  137. var height: ConstraintExtendable { get }
  138. var centerX: ConstraintExtendable { get }
  139. var centerY: ConstraintExtendable { get }
  140. var baseline: ConstraintExtendable { get }
  141. #if os(iOS)
  142. var firstBaseline: ConstraintExtendable { get }
  143. var leftMargin: ConstraintExtendable { get }
  144. var rightMargin: ConstraintExtendable { get }
  145. var topMargin: ConstraintExtendable { get }
  146. var bottomMargin: ConstraintExtendable { get }
  147. var leadingMargin: ConstraintExtendable { get }
  148. var trailingMargin: ConstraintExtendable { get }
  149. var centerXWithinMargins: ConstraintExtendable { get }
  150. var centerYWithinMargins: ConstraintExtendable { get }
  151. #endif
  152. }
  153. /**
  154. * MutableConstraint is a single item that defines all the properties for a single ConstraintMaker chain
  155. */
  156. final internal class MutableConstraint: Constraint, ConstraintExtendable, ConstraintOffsetable, ConstraintFinalizable {
  157. var left: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Left) }
  158. var top: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Top) }
  159. var right: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Right) }
  160. var bottom: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Bottom) }
  161. var leading: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Leading) }
  162. var trailing: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Trailing) }
  163. var width: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Width) }
  164. var height: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Height) }
  165. var centerX: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterX) }
  166. var centerY: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterY) }
  167. var baseline: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Baseline) }
  168. #if os(iOS)
  169. var firstBaseline: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) }
  170. var leftMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) }
  171. var rightMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) }
  172. var topMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) }
  173. var bottomMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) }
  174. var leadingMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) }
  175. var trailingMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) }
  176. var centerXWithinMargins: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) }
  177. var centerYWithinMargins: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) }
  178. #endif
  179. // MARK: initializer
  180. init(fromItem: ConstraintItem) {
  181. self.fromItem = fromItem
  182. self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
  183. }
  184. // MARK: equalTo
  185. func equalTo(other: ConstraintItem) -> ConstraintOffsetable {
  186. return self.constrainTo(other, relation: .Equal)
  187. }
  188. func equalTo(other: View) -> ConstraintOffsetable {
  189. return self.constrainTo(other, relation: .Equal)
  190. }
  191. #if os(iOS)
  192. func equalTo(other: UILayoutSupport) -> ConstraintOffsetable {
  193. return self.constrainTo(other, relation: .Equal)
  194. }
  195. #endif
  196. func equalTo(other: Float) -> ConstraintMultipliable {
  197. return self.constrainTo(other, relation: .Equal)
  198. }
  199. func equalTo(other: Double) -> ConstraintMultipliable {
  200. return self.constrainTo(Float(other), relation: .Equal)
  201. }
  202. func equalTo(other: CGFloat) -> ConstraintMultipliable {
  203. return self.constrainTo(Float(other), relation: .Equal)
  204. }
  205. func equalTo(other: Int) -> ConstraintMultipliable {
  206. return self.constrainTo(Float(other), relation: .Equal)
  207. }
  208. func equalTo(other: UInt) -> ConstraintMultipliable {
  209. return self.constrainTo(Float(other), relation: .Equal)
  210. }
  211. func equalTo(other: CGSize) -> ConstraintMultipliable {
  212. return self.constrainTo(other, relation: .Equal)
  213. }
  214. func equalTo(other: CGPoint) -> ConstraintMultipliable {
  215. return self.constrainTo(other, relation: .Equal)
  216. }
  217. func equalTo(other: EdgeInsets) -> ConstraintMultipliable {
  218. return self.constrainTo(other, relation: .Equal)
  219. }
  220. // MARK: lessThanOrEqualTo
  221. func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable {
  222. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  223. }
  224. func lessThanOrEqualTo(other: View) -> ConstraintOffsetable {
  225. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  226. }
  227. #if os(iOS)
  228. func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable {
  229. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  230. }
  231. #endif
  232. func lessThanOrEqualTo(other: Float) -> ConstraintMultipliable {
  233. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  234. }
  235. func lessThanOrEqualTo(other: Double) -> ConstraintMultipliable {
  236. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  237. }
  238. func lessThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable {
  239. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  240. }
  241. func lessThanOrEqualTo(other: Int) -> ConstraintMultipliable {
  242. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  243. }
  244. func lessThanOrEqualTo(other: UInt) -> ConstraintMultipliable {
  245. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  246. }
  247. func lessThanOrEqualTo(other: CGSize) -> ConstraintMultipliable {
  248. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  249. }
  250. func lessThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable {
  251. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  252. }
  253. func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable {
  254. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  255. }
  256. // MARK: greaterThanOrEqualTo
  257. func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable {
  258. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  259. }
  260. func greaterThanOrEqualTo(other: View) -> ConstraintOffsetable {
  261. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  262. }
  263. #if os(iOS)
  264. func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable {
  265. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  266. }
  267. #endif
  268. func greaterThanOrEqualTo(other: Float) -> ConstraintMultipliable {
  269. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  270. }
  271. func greaterThanOrEqualTo(other: Double) -> ConstraintMultipliable {
  272. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  273. }
  274. func greaterThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable {
  275. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  276. }
  277. func greaterThanOrEqualTo(other: Int) -> ConstraintMultipliable {
  278. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  279. }
  280. func greaterThanOrEqualTo(other: UInt) -> ConstraintMultipliable {
  281. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  282. }
  283. func greaterThanOrEqualTo(other: CGSize) -> ConstraintMultipliable {
  284. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  285. }
  286. func greaterThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable {
  287. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  288. }
  289. func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable {
  290. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  291. }
  292. // MARK: multiplier
  293. func multipliedBy(amount: Float) -> ConstraintPriortizable {
  294. self.multiplier = amount
  295. return self
  296. }
  297. func multipliedBy(amount: Double) -> ConstraintPriortizable {
  298. return self.multipliedBy(Float(amount))
  299. }
  300. func multipliedBy(amount: CGFloat) -> ConstraintPriortizable {
  301. return self.multipliedBy(Float(amount))
  302. }
  303. func multipliedBy(amount: Int) -> ConstraintPriortizable {
  304. return self.multipliedBy(Float(amount))
  305. }
  306. func multipliedBy(amount: UInt) -> ConstraintPriortizable {
  307. return self.multipliedBy(Float(amount))
  308. }
  309. func dividedBy(amount: Float) -> ConstraintPriortizable {
  310. self.multiplier = 1.0 / amount;
  311. return self
  312. }
  313. func dividedBy(amount: Double) -> ConstraintPriortizable {
  314. return self.dividedBy(Float(amount))
  315. }
  316. func dividedBy(amount: CGFloat) -> ConstraintPriortizable {
  317. return self.dividedBy(Float(amount))
  318. }
  319. func dividedBy(amount: Int) -> ConstraintPriortizable {
  320. return self.dividedBy(Float(amount))
  321. }
  322. func dividedBy(amount: UInt) -> ConstraintPriortizable {
  323. return self.dividedBy(Float(amount))
  324. }
  325. // MARK: priority
  326. func priority(priority: Float) -> ConstraintFinalizable {
  327. self.priority = priority
  328. return self
  329. }
  330. func priority(priority: Double) -> ConstraintFinalizable {
  331. return self.priority(Float(priority))
  332. }
  333. func priority(priority: CGFloat) -> ConstraintFinalizable {
  334. return self.priority(Float(priority))
  335. }
  336. func priority(priority: UInt) -> ConstraintFinalizable {
  337. return self.priority(Float(priority))
  338. }
  339. func priority(priority: Int) -> ConstraintFinalizable {
  340. return self.priority(Float(priority))
  341. }
  342. func priorityRequired() -> ConstraintFinalizable {
  343. return self.priority(1000.0)
  344. }
  345. func priorityHigh() -> ConstraintFinalizable {
  346. return self.priority(750.0)
  347. }
  348. func priorityMedium() -> ConstraintFinalizable {
  349. #if os(iOS)
  350. return self.priority(500.0)
  351. #else
  352. return self.priority(501.0)
  353. #endif
  354. }
  355. func priorityLow() -> ConstraintFinalizable {
  356. return self.priority(250.0)
  357. }
  358. // MARK: offset
  359. func offset(amount: Float) -> ConstraintMultipliable {
  360. self.offset = amount
  361. return self
  362. }
  363. func offset(amount: Double) -> ConstraintMultipliable {
  364. return self.offset(Float(amount))
  365. }
  366. func offset(amount: CGFloat) -> ConstraintMultipliable {
  367. return self.offset(Float(amount))
  368. }
  369. func offset(amount: Int) -> ConstraintMultipliable {
  370. return self.offset(Float(amount))
  371. }
  372. func offset(amount: UInt) -> ConstraintMultipliable {
  373. return self.offset(Float(amount))
  374. }
  375. func offset(amount: CGPoint) -> ConstraintMultipliable {
  376. self.offset = amount
  377. return self
  378. }
  379. func offset(amount: CGSize) -> ConstraintMultipliable {
  380. self.offset = amount
  381. return self
  382. }
  383. func offset(amount: EdgeInsets) -> ConstraintMultipliable {
  384. self.offset = amount
  385. return self
  386. }
  387. // MARK: insets
  388. func insets(amount: EdgeInsets) -> ConstraintMultipliable {
  389. self.offset = amount
  390. return self
  391. }
  392. // MARK: Constraint
  393. var constraint: Constraint {
  394. return self
  395. }
  396. // MARK: install / uninstall
  397. func install() -> [LayoutConstraint] {
  398. return self.installOnView(updateExisting: false)
  399. }
  400. func uninstall() {
  401. self.uninstallFromView()
  402. }
  403. func activate() {
  404. if NSLayoutConstraint.respondsToSelector("activateConstraints:") && self.installInfo != nil {
  405. let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint]
  406. if layoutConstraints.count > 0 {
  407. NSLayoutConstraint.activateConstraints(layoutConstraints)
  408. }
  409. } else {
  410. self.install()
  411. }
  412. }
  413. func deactivate() {
  414. if NSLayoutConstraint.respondsToSelector("deactivateConstraints:") && self.installInfo != nil {
  415. let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint]
  416. if layoutConstraints.count > 0 {
  417. NSLayoutConstraint.deactivateConstraints(layoutConstraints)
  418. }
  419. } else {
  420. self.uninstall()
  421. }
  422. }
  423. func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] {
  424. var installOnView: View? = nil
  425. if self.toItem.view != nil {
  426. installOnView = MutableConstraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
  427. if installOnView == nil {
  428. NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
  429. return []
  430. }
  431. } else {
  432. installOnView = self.fromItem.view?.superview
  433. if installOnView == nil {
  434. if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
  435. installOnView = self.fromItem.view
  436. }
  437. if installOnView == nil {
  438. NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
  439. return []
  440. }
  441. }
  442. }
  443. if let installedOnView = self.installInfo?.view {
  444. if installedOnView != installOnView {
  445. NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise()
  446. return []
  447. }
  448. return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? []
  449. }
  450. var newLayoutConstraints = [LayoutConstraint]()
  451. let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
  452. let layoutToAttributes = self.toItem.attributes.layoutAttributes
  453. // get layout from
  454. let layoutFrom: View? = self.fromItem.view
  455. // get layout relation
  456. let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
  457. for layoutFromAttribute in layoutFromAttributes {
  458. // get layout to attribute
  459. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  460. // get layout constant
  461. var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
  462. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  463. // get layout to
  464. var layoutTo: View? = self.toItem.view
  465. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  466. layoutTo = installOnView
  467. }
  468. // create layout constraint
  469. let layoutConstraint = LayoutConstraint(
  470. item: layoutFrom!,
  471. attribute: layoutFromAttribute,
  472. relatedBy: layoutRelation,
  473. toItem: layoutTo,
  474. attribute: layoutToAttribute,
  475. multiplier: CGFloat(self.multiplier),
  476. constant: layoutConstant)
  477. // set priority
  478. layoutConstraint.priority = self.priority
  479. // set constraint
  480. layoutConstraint.snp_constraint = self
  481. newLayoutConstraints.append(layoutConstraint)
  482. }
  483. // special logic for updating
  484. if updateExisting {
  485. // get existing constraints for this view
  486. let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
  487. // array that will contain only new layout constraints to keep
  488. var newLayoutConstraintsToKeep = [LayoutConstraint]()
  489. // begin looping
  490. for layoutConstraint in newLayoutConstraints {
  491. // layout constraint that should be updated
  492. var updateLayoutConstraint: LayoutConstraint? = nil
  493. // loop through existing and check for match
  494. for existingLayoutConstraint in existingLayoutConstraints {
  495. if existingLayoutConstraint == layoutConstraint {
  496. updateLayoutConstraint = existingLayoutConstraint
  497. break
  498. }
  499. }
  500. // if we have existing one lets just update the constant
  501. if updateLayoutConstraint != nil {
  502. updateLayoutConstraint!.constant = layoutConstraint.constant
  503. }
  504. // otherwise add this layout constraint to new keep list
  505. else {
  506. newLayoutConstraintsToKeep.append(layoutConstraint)
  507. }
  508. }
  509. // set constraints to only new ones
  510. newLayoutConstraints = newLayoutConstraintsToKeep
  511. }
  512. // add constraints
  513. installOnView!.addConstraints(newLayoutConstraints)
  514. // set install info
  515. self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable())
  516. // store which layout constraints are installed for this constraint
  517. for layoutConstraint in newLayoutConstraints {
  518. self.installInfo!.layoutConstraints.addObject(layoutConstraint)
  519. }
  520. // store the layout constraints against the layout from view
  521. layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints
  522. // return the new constraints
  523. return newLayoutConstraints
  524. }
  525. func uninstallFromView() {
  526. if let installInfo = self.installInfo,
  527. let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] {
  528. if installedLayoutConstraints.count > 0 {
  529. if let installedOnView = installInfo.view {
  530. // remove the constraints from the UIView's storage
  531. installedOnView.removeConstraints(installedLayoutConstraints)
  532. }
  533. // remove the constraints from the from item view
  534. if let fromView = self.fromItem.view {
  535. fromView.snp_installedLayoutConstraints = fromView.snp_installedLayoutConstraints.filter {
  536. return !contains(installedLayoutConstraints, $0)
  537. }
  538. }
  539. }
  540. }
  541. self.installInfo = nil
  542. }
  543. // MARK: private
  544. private let fromItem: ConstraintItem
  545. private var toItem: ConstraintItem
  546. private var relation: ConstraintRelation?
  547. private var constant: Any = Float(0.0)
  548. private var multiplier: Float = 1.0
  549. private var priority: Float = 1000.0
  550. private var offset: Any = Float(0.0)
  551. private var installInfo: ConstraintInstallInfo?
  552. private func addConstraint(attributes: ConstraintAttributes) -> MutableConstraint {
  553. if self.relation == nil {
  554. self.fromItem.attributes += attributes
  555. }
  556. return self
  557. }
  558. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> MutableConstraint {
  559. if other.attributes != ConstraintAttributes.None {
  560. let toLayoutAttributes = other.attributes.layoutAttributes
  561. if toLayoutAttributes.count > 1 {
  562. let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  563. if toLayoutAttributes != fromLayoutAttributes {
  564. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  565. return self
  566. }
  567. other.attributes = ConstraintAttributes.None
  568. }
  569. }
  570. self.toItem = other
  571. self.relation = relation
  572. return self
  573. }
  574. private func constrainTo(other: View, relation: ConstraintRelation) -> MutableConstraint {
  575. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  576. }
  577. #if os(iOS)
  578. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> MutableConstraint {
  579. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  580. }
  581. #endif
  582. private func constrainTo(other: Float, relation: ConstraintRelation) -> MutableConstraint {
  583. self.constant = other
  584. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  585. }
  586. private func constrainTo(other: Double, relation: ConstraintRelation) -> MutableConstraint {
  587. self.constant = other
  588. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  589. }
  590. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> MutableConstraint {
  591. self.constant = other
  592. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  593. }
  594. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> MutableConstraint {
  595. self.constant = other
  596. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  597. }
  598. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> MutableConstraint {
  599. self.constant = other
  600. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  601. }
  602. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  603. var views = Set<View>()
  604. var fromView = fromView
  605. var toView = toView
  606. do {
  607. if let view = toView {
  608. if views.contains(view) {
  609. return view
  610. }
  611. views.insert(view)
  612. toView = view.superview
  613. }
  614. if let view = fromView {
  615. if views.contains(view) {
  616. return view
  617. }
  618. views.insert(view)
  619. fromView = view.superview
  620. }
  621. } while (fromView != nil || toView != nil)
  622. return nil
  623. }
  624. }
  625. private extension NSLayoutAttribute {
  626. private func snp_offsetForValue(value: Any?) -> CGFloat {
  627. // Float
  628. if let float = value as? Float {
  629. return CGFloat(float)
  630. }
  631. // Double
  632. else if let double = value as? Double {
  633. return CGFloat(double)
  634. }
  635. // UInt
  636. else if let int = value as? Int {
  637. return CGFloat(int)
  638. }
  639. // Int
  640. else if let uint = value as? UInt {
  641. return CGFloat(uint)
  642. }
  643. // CGFloat
  644. else if let float = value as? CGFloat {
  645. return float
  646. }
  647. // CGSize
  648. else if let size = value as? CGSize {
  649. if self == .Width {
  650. return size.width
  651. } else if self == .Height {
  652. return size.height
  653. }
  654. }
  655. // CGPoint
  656. else if let point = value as? CGPoint {
  657. #if os(iOS)
  658. switch self {
  659. case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x
  660. case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y
  661. case .Right, .RightMargin: return -point.x
  662. case .Bottom, .BottomMargin: return -point.y
  663. case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
  664. case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
  665. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  666. }
  667. #else
  668. switch self {
  669. case .Left, .CenterX: return point.x
  670. case .Top, .CenterY, .Baseline: return point.y
  671. case .Right: return -point.x
  672. case .Bottom: return -point.y
  673. case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
  674. case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
  675. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  676. }
  677. #endif
  678. }
  679. // EdgeInsets
  680. else if let insets = value as? EdgeInsets {
  681. #if os(iOS)
  682. switch self {
  683. case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left
  684. case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top
  685. case .Right, .RightMargin: return -insets.right
  686. case .Bottom, .BottomMargin: return -insets.bottom
  687. case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
  688. case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
  689. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  690. }
  691. #else
  692. switch self {
  693. case .Left, .CenterX: return insets.left
  694. case .Top, .CenterY, .Baseline: return insets.top
  695. case .Right: return -insets.right
  696. case .Bottom: return -insets.bottom
  697. case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
  698. case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
  699. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  700. }
  701. #endif
  702. }
  703. return CGFloat(0)
  704. }
  705. private func snp_constantForValue(value: Any?) -> CGFloat {
  706. // Float
  707. if let float = value as? Float {
  708. return CGFloat(float)
  709. }
  710. // Double
  711. else if let double = value as? Double {
  712. return CGFloat(double)
  713. }
  714. // UInt
  715. else if let int = value as? Int {
  716. return CGFloat(int)
  717. }
  718. // Int
  719. else if let uint = value as? UInt {
  720. return CGFloat(uint)
  721. }
  722. // CGFloat
  723. else if let float = value as? CGFloat {
  724. return float
  725. }
  726. // CGSize
  727. else if let size = value as? CGSize {
  728. if self == .Width {
  729. return size.width
  730. } else if self == .Height {
  731. return size.height
  732. }
  733. }
  734. // CGPoint
  735. else if let point = value as? CGPoint {
  736. #if os(iOS)
  737. switch self {
  738. case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x
  739. case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y
  740. case .Right, .RightMargin: return point.x
  741. case .Bottom, .BottomMargin: return point.y
  742. case .Leading, .LeadingMargin: return point.x
  743. case .Trailing, .TrailingMargin: return point.x
  744. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  745. }
  746. #else
  747. switch self {
  748. case .Left, .CenterX: return point.x
  749. case .Top, .CenterY, .Baseline: return point.y
  750. case .Right: return point.x
  751. case .Bottom: return point.y
  752. case .Leading: return point.x
  753. case .Trailing: return point.x
  754. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  755. }
  756. #endif
  757. }
  758. // EdgeInsets
  759. else if let insets = value as? EdgeInsets {
  760. #if os(iOS)
  761. switch self {
  762. case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left
  763. case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top
  764. case .Right, .RightMargin: return insets.right
  765. case .Bottom, .BottomMargin: return insets.bottom
  766. case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right
  767. case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left
  768. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  769. }
  770. #else
  771. switch self {
  772. case .Left, .CenterX: return insets.left
  773. case .Top, .CenterY, .Baseline: return insets.top
  774. case .Right: return insets.right
  775. case .Bottom: return insets.bottom
  776. case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right
  777. case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left
  778. case .Width, .Height, .NotAnAttribute: return CGFloat(0)
  779. }
  780. #endif
  781. }
  782. return CGFloat(0);
  783. }
  784. }
  785. private struct ConstraintInstallInfo {
  786. weak var view: View? = nil
  787. let layoutConstraints: NSHashTable
  788. }
  789. internal func ==(left: MutableConstraint, right: MutableConstraint) -> Bool {
  790. return (left.fromItem == right.fromItem &&
  791. left.toItem == right.toItem &&
  792. left.relation == right.relation &&
  793. left.multiplier == right.multiplier &&
  794. left.priority == right.priority)
  795. }