ConstraintDescription.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //
  2. // SnapKit
  3. //
  4. // Copyright (c) 2011-2015 SnapKit Team - https://github.com/SnapKit
  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. Used to expose the final API of a `ConstraintDescription` which allows getting a constraint from it
  30. */
  31. public protocol ConstraintDescriptionFinalizable: class {
  32. var constraint: Constraint { get }
  33. }
  34. /**
  35. Used to expose priority APIs
  36. */
  37. public protocol ConstraintDescriptionPriortizable: ConstraintDescriptionFinalizable {
  38. func priority(priority: Float) -> ConstraintDescriptionFinalizable
  39. func priority(priority: Double) -> ConstraintDescriptionFinalizable
  40. func priority(priority: CGFloat) -> ConstraintDescriptionFinalizable
  41. func priority(priority: UInt) -> ConstraintDescriptionFinalizable
  42. func priority(priority: Int) -> ConstraintDescriptionFinalizable
  43. func priorityRequired() -> ConstraintDescriptionFinalizable
  44. func priorityHigh() -> ConstraintDescriptionFinalizable
  45. func priorityMedium() -> ConstraintDescriptionFinalizable
  46. func priorityLow() -> ConstraintDescriptionFinalizable
  47. }
  48. /**
  49. Used to expose multiplier & constant APIs
  50. */
  51. public protocol ConstraintDescriptionEditable: ConstraintDescriptionPriortizable {
  52. func multipliedBy(amount: Float) -> ConstraintDescriptionEditable
  53. func multipliedBy(amount: Double) -> ConstraintDescriptionEditable
  54. func multipliedBy(amount: CGFloat) -> ConstraintDescriptionEditable
  55. func multipliedBy(amount: Int) -> ConstraintDescriptionEditable
  56. func multipliedBy(amount: UInt) -> ConstraintDescriptionEditable
  57. func dividedBy(amount: Float) -> ConstraintDescriptionEditable
  58. func dividedBy(amount: Double) -> ConstraintDescriptionEditable
  59. func dividedBy(amount: CGFloat) -> ConstraintDescriptionEditable
  60. func dividedBy(amount: Int) -> ConstraintDescriptionEditable
  61. func dividedBy(amount: UInt) -> ConstraintDescriptionEditable
  62. func offset(amount: Float) -> ConstraintDescriptionEditable
  63. func offset(amount: Double) -> ConstraintDescriptionEditable
  64. func offset(amount: CGFloat) -> ConstraintDescriptionEditable
  65. func offset(amount: Int) -> ConstraintDescriptionEditable
  66. func offset(amount: UInt) -> ConstraintDescriptionEditable
  67. func offset(amount: CGPoint) -> ConstraintDescriptionEditable
  68. func offset(amount: CGSize) -> ConstraintDescriptionEditable
  69. func offset(amount: EdgeInsets) -> ConstraintDescriptionEditable
  70. func insets(amount: EdgeInsets) -> ConstraintDescriptionEditable
  71. }
  72. /**
  73. Used to expose relation APIs
  74. */
  75. public protocol ConstraintDescriptionRelatable: class {
  76. func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable
  77. func equalTo(other: View) -> ConstraintDescriptionEditable
  78. #if os(iOS)
  79. func equalTo(other: UILayoutSupport) -> ConstraintDescriptionEditable
  80. #endif
  81. func equalTo(other: Float) -> ConstraintDescriptionEditable
  82. func equalTo(other: Double) -> ConstraintDescriptionEditable
  83. func equalTo(other: CGFloat) -> ConstraintDescriptionEditable
  84. func equalTo(other: Int) -> ConstraintDescriptionEditable
  85. func equalTo(other: UInt) -> ConstraintDescriptionEditable
  86. func equalTo(other: CGSize) -> ConstraintDescriptionEditable
  87. func equalTo(other: CGPoint) -> ConstraintDescriptionEditable
  88. func equalTo(other: EdgeInsets) -> ConstraintDescriptionEditable
  89. func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
  90. func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable
  91. #if os(iOS)
  92. func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionEditable
  93. #endif
  94. func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable
  95. func lessThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable
  96. func lessThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable
  97. func lessThanOrEqualTo(other: Int) -> ConstraintDescriptionEditable
  98. func lessThanOrEqualTo(other: UInt) -> ConstraintDescriptionEditable
  99. func lessThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable
  100. func lessThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable
  101. func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable
  102. func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
  103. func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable
  104. #if os(iOS)
  105. func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionEditable
  106. #endif
  107. func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable
  108. func greaterThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable
  109. func greaterThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable
  110. func greaterThanOrEqualTo(other: Int) -> ConstraintDescriptionEditable
  111. func greaterThanOrEqualTo(other: UInt) -> ConstraintDescriptionEditable
  112. func greaterThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable
  113. func greaterThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable
  114. func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable
  115. }
  116. /**
  117. Used to expose chaining APIs
  118. */
  119. public protocol ConstraintDescriptionExtendable: ConstraintDescriptionRelatable {
  120. var left: ConstraintDescriptionExtendable { get }
  121. var top: ConstraintDescriptionExtendable { get }
  122. var bottom: ConstraintDescriptionExtendable { get }
  123. var right: ConstraintDescriptionExtendable { get }
  124. var leading: ConstraintDescriptionExtendable { get }
  125. var trailing: ConstraintDescriptionExtendable { get }
  126. var width: ConstraintDescriptionExtendable { get }
  127. var height: ConstraintDescriptionExtendable { get }
  128. var centerX: ConstraintDescriptionExtendable { get }
  129. var centerY: ConstraintDescriptionExtendable { get }
  130. var baseline: ConstraintDescriptionExtendable { get }
  131. #if os(iOS)
  132. var firstBaseline: ConstraintDescriptionExtendable { get }
  133. var leftMargin: ConstraintDescriptionExtendable { get }
  134. var rightMargin: ConstraintDescriptionExtendable { get }
  135. var topMargin: ConstraintDescriptionExtendable { get }
  136. var bottomMargin: ConstraintDescriptionExtendable { get }
  137. var leadingMargin: ConstraintDescriptionExtendable { get }
  138. var trailingMargin: ConstraintDescriptionExtendable { get }
  139. var centerXWithinMargins: ConstraintDescriptionExtendable { get }
  140. var centerYWithinMargins: ConstraintDescriptionExtendable { get }
  141. #endif
  142. }
  143. /**
  144. Used to internally manage building constraint
  145. */
  146. final internal class ConstraintDescription: ConstraintDescriptionExtendable, ConstraintDescriptionEditable, ConstraintDescriptionFinalizable {
  147. internal var left: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Left) }
  148. internal var top: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Top) }
  149. internal var right: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Right) }
  150. internal var bottom: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Bottom) }
  151. internal var leading: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Leading) }
  152. internal var trailing: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Trailing) }
  153. internal var width: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Width) }
  154. internal var height: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Height) }
  155. internal var centerX: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterX) }
  156. internal var centerY: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterY) }
  157. internal var baseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Baseline) }
  158. #if os(iOS)
  159. internal var firstBaseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) }
  160. internal var leftMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) }
  161. internal var rightMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) }
  162. internal var topMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) }
  163. internal var bottomMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) }
  164. internal var leadingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) }
  165. internal var trailingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) }
  166. internal var centerXWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) }
  167. internal var centerYWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) }
  168. #endif
  169. // MARK: initializer
  170. init(fromItem: ConstraintItem) {
  171. self.fromItem = fromItem
  172. self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
  173. }
  174. // MARK: equalTo
  175. internal func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
  176. return self.constrainTo(other, relation: .Equal)
  177. }
  178. internal func equalTo(other: View) -> ConstraintDescriptionEditable {
  179. return self.constrainTo(other, relation: .Equal)
  180. }
  181. #if os(iOS)
  182. internal func equalTo(other: UILayoutSupport) -> ConstraintDescriptionEditable {
  183. return self.constrainTo(other, relation: .Equal)
  184. }
  185. #endif
  186. internal func equalTo(other: Float) -> ConstraintDescriptionEditable {
  187. return self.constrainTo(other, relation: .Equal)
  188. }
  189. internal func equalTo(other: Double) -> ConstraintDescriptionEditable {
  190. return self.constrainTo(Float(other), relation: .Equal)
  191. }
  192. internal func equalTo(other: CGFloat) -> ConstraintDescriptionEditable {
  193. return self.constrainTo(Float(other), relation: .Equal)
  194. }
  195. internal func equalTo(other: Int) -> ConstraintDescriptionEditable {
  196. return self.constrainTo(Float(other), relation: .Equal)
  197. }
  198. internal func equalTo(other: UInt) -> ConstraintDescriptionEditable {
  199. return self.constrainTo(Float(other), relation: .Equal)
  200. }
  201. internal func equalTo(other: CGSize) -> ConstraintDescriptionEditable {
  202. return self.constrainTo(other, relation: .Equal)
  203. }
  204. internal func equalTo(other: CGPoint) -> ConstraintDescriptionEditable {
  205. return self.constrainTo(other, relation: .Equal)
  206. }
  207. internal func equalTo(other: EdgeInsets) -> ConstraintDescriptionEditable {
  208. return self.constrainTo(other, relation: .Equal)
  209. }
  210. // MARK: lessThanOrEqualTo
  211. internal func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
  212. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  213. }
  214. internal func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable {
  215. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  216. }
  217. #if os(iOS)
  218. internal func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionEditable {
  219. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  220. }
  221. #endif
  222. internal func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable {
  223. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  224. }
  225. internal func lessThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable {
  226. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  227. }
  228. internal func lessThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable {
  229. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  230. }
  231. internal func lessThanOrEqualTo(other: Int) -> ConstraintDescriptionEditable {
  232. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  233. }
  234. internal func lessThanOrEqualTo(other: UInt) -> ConstraintDescriptionEditable {
  235. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  236. }
  237. internal func lessThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable {
  238. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  239. }
  240. internal func lessThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable {
  241. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  242. }
  243. internal func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable {
  244. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  245. }
  246. // MARK: greaterThanOrEqualTo
  247. internal func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
  248. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  249. }
  250. internal func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable {
  251. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  252. }
  253. #if os(iOS)
  254. internal func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionEditable {
  255. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  256. }
  257. #endif
  258. internal func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable {
  259. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  260. }
  261. internal func greaterThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable {
  262. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  263. }
  264. internal func greaterThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable {
  265. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  266. }
  267. internal func greaterThanOrEqualTo(other: Int) -> ConstraintDescriptionEditable {
  268. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  269. }
  270. internal func greaterThanOrEqualTo(other: UInt) -> ConstraintDescriptionEditable {
  271. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  272. }
  273. internal func greaterThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable {
  274. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  275. }
  276. internal func greaterThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable {
  277. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  278. }
  279. internal func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable {
  280. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  281. }
  282. // MARK: multiplier
  283. internal func multipliedBy(amount: Float) -> ConstraintDescriptionEditable {
  284. self.multiplier = amount
  285. return self
  286. }
  287. internal func multipliedBy(amount: Double) -> ConstraintDescriptionEditable {
  288. return self.multipliedBy(Float(amount))
  289. }
  290. internal func multipliedBy(amount: CGFloat) -> ConstraintDescriptionEditable {
  291. return self.multipliedBy(Float(amount))
  292. }
  293. internal func multipliedBy(amount: Int) -> ConstraintDescriptionEditable {
  294. return self.multipliedBy(Float(amount))
  295. }
  296. internal func multipliedBy(amount: UInt) -> ConstraintDescriptionEditable {
  297. return self.multipliedBy(Float(amount))
  298. }
  299. internal func dividedBy(amount: Float) -> ConstraintDescriptionEditable {
  300. self.multiplier = 1.0 / amount;
  301. return self
  302. }
  303. internal func dividedBy(amount: Double) -> ConstraintDescriptionEditable {
  304. return self.dividedBy(Float(amount))
  305. }
  306. internal func dividedBy(amount: CGFloat) -> ConstraintDescriptionEditable {
  307. return self.dividedBy(Float(amount))
  308. }
  309. internal func dividedBy(amount: Int) -> ConstraintDescriptionEditable {
  310. return self.dividedBy(Float(amount))
  311. }
  312. internal func dividedBy(amount: UInt) -> ConstraintDescriptionEditable {
  313. return self.dividedBy(Float(amount))
  314. }
  315. // MARK: offset
  316. internal func offset(amount: Float) -> ConstraintDescriptionEditable {
  317. self.constant = amount
  318. return self
  319. }
  320. internal func offset(amount: Double) -> ConstraintDescriptionEditable {
  321. return self.offset(Float(amount))
  322. }
  323. internal func offset(amount: CGFloat) -> ConstraintDescriptionEditable {
  324. return self.offset(Float(amount))
  325. }
  326. internal func offset(amount: Int) -> ConstraintDescriptionEditable {
  327. return self.offset(Float(amount))
  328. }
  329. internal func offset(amount: UInt) -> ConstraintDescriptionEditable {
  330. return self.offset(Float(amount))
  331. }
  332. internal func offset(amount: CGPoint) -> ConstraintDescriptionEditable {
  333. self.constant = amount
  334. return self
  335. }
  336. internal func offset(amount: CGSize) -> ConstraintDescriptionEditable {
  337. self.constant = amount
  338. return self
  339. }
  340. internal func offset(amount: EdgeInsets) -> ConstraintDescriptionEditable {
  341. self.constant = amount
  342. return self
  343. }
  344. // MARK: insets
  345. internal func insets(amount: EdgeInsets) -> ConstraintDescriptionEditable {
  346. self.constant = EdgeInsets(top: amount.top, left: amount.left, bottom: -amount.bottom, right: -amount.right)
  347. return self
  348. }
  349. // MARK: priority
  350. internal func priority(priority: Float) -> ConstraintDescriptionFinalizable {
  351. self.priority = priority
  352. return self
  353. }
  354. internal func priority(priority: Double) -> ConstraintDescriptionFinalizable {
  355. return self.priority(Float(priority))
  356. }
  357. internal func priority(priority: CGFloat) -> ConstraintDescriptionFinalizable {
  358. return self.priority(Float(priority))
  359. }
  360. func priority(priority: UInt) -> ConstraintDescriptionFinalizable {
  361. return self.priority(Float(priority))
  362. }
  363. internal func priority(priority: Int) -> ConstraintDescriptionFinalizable {
  364. return self.priority(Float(priority))
  365. }
  366. internal func priorityRequired() -> ConstraintDescriptionFinalizable {
  367. return self.priority(1000.0)
  368. }
  369. internal func priorityHigh() -> ConstraintDescriptionFinalizable {
  370. return self.priority(750.0)
  371. }
  372. internal func priorityMedium() -> ConstraintDescriptionFinalizable {
  373. #if os(iOS)
  374. return self.priority(500.0)
  375. #else
  376. return self.priority(501.0)
  377. #endif
  378. }
  379. internal func priorityLow() -> ConstraintDescriptionFinalizable {
  380. return self.priority(250.0)
  381. }
  382. // MARK: Constraint
  383. internal var constraint: Constraint {
  384. if self.concreteConstraint == nil {
  385. if self.relation == nil {
  386. fatalError("Attempting to create a constraint from a ConstraintDescription before it has been fully chained.")
  387. }
  388. self.concreteConstraint = ConcreteConstraint(
  389. fromItem: self.fromItem,
  390. toItem: self.toItem,
  391. relation: self.relation!,
  392. constant: self.constant,
  393. multiplier: self.multiplier,
  394. priority: self.priority)
  395. }
  396. return self.concreteConstraint!
  397. }
  398. // MARK: Private
  399. private let fromItem: ConstraintItem
  400. private var toItem: ConstraintItem {
  401. willSet {
  402. if self.concreteConstraint != nil {
  403. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  404. }
  405. }
  406. }
  407. private var relation: ConstraintRelation? {
  408. willSet {
  409. if self.concreteConstraint != nil {
  410. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  411. }
  412. }
  413. }
  414. private var constant: Any = Float(0.0) {
  415. willSet {
  416. if self.concreteConstraint != nil {
  417. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  418. }
  419. }
  420. }
  421. private var multiplier: Float = 1.0 {
  422. willSet {
  423. if self.concreteConstraint != nil {
  424. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  425. }
  426. }
  427. }
  428. private var priority: Float = 1000.0 {
  429. willSet {
  430. if self.concreteConstraint != nil {
  431. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  432. }
  433. }
  434. }
  435. private var concreteConstraint: ConcreteConstraint? = nil
  436. private func addConstraint(attributes: ConstraintAttributes) -> ConstraintDescription {
  437. if self.relation == nil {
  438. self.fromItem.attributes += attributes
  439. }
  440. return self
  441. }
  442. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> ConstraintDescription {
  443. if other.attributes != ConstraintAttributes.None {
  444. let toLayoutAttributes = other.attributes.layoutAttributes
  445. if toLayoutAttributes.count > 1 {
  446. let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  447. if toLayoutAttributes != fromLayoutAttributes {
  448. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  449. return self
  450. }
  451. other.attributes = ConstraintAttributes.None
  452. }
  453. }
  454. self.toItem = other
  455. self.relation = relation
  456. return self
  457. }
  458. private func constrainTo(other: View, relation: ConstraintRelation) -> ConstraintDescription {
  459. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  460. }
  461. #if os(iOS)
  462. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> ConstraintDescription {
  463. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  464. }
  465. #endif
  466. private func constrainTo(other: Float, relation: ConstraintRelation) -> ConstraintDescription {
  467. self.constant = other
  468. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  469. }
  470. private func constrainTo(other: Double, relation: ConstraintRelation) -> ConstraintDescription {
  471. self.constant = other
  472. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  473. }
  474. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> ConstraintDescription {
  475. self.constant = other
  476. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  477. }
  478. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> ConstraintDescription {
  479. self.constant = other
  480. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  481. }
  482. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> ConstraintDescription {
  483. self.constant = other
  484. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  485. }
  486. }