Constraint.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. //
  2. // Snappy
  3. //
  4. // Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #if os(iOS)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. /**
  29. * Constraint is a single item that defines all the properties for a single ConstraintMaker chain
  30. */
  31. public class Constraint {
  32. public var left: Constraint { return addConstraint(ConstraintAttributes.Left) }
  33. public var top: Constraint { return addConstraint(ConstraintAttributes.Top) }
  34. public var right: Constraint { return addConstraint(ConstraintAttributes.Right) }
  35. public var bottom: Constraint { return addConstraint(ConstraintAttributes.Bottom) }
  36. public var leading: Constraint { return addConstraint(ConstraintAttributes.Leading) }
  37. public var trailing: Constraint { return addConstraint(ConstraintAttributes.Trailing) }
  38. public var width: Constraint { return addConstraint(ConstraintAttributes.Width) }
  39. public var height: Constraint { return addConstraint(ConstraintAttributes.Height) }
  40. public var centerX: Constraint { return addConstraint(ConstraintAttributes.CenterX) }
  41. public var centerY: Constraint { return addConstraint(ConstraintAttributes.CenterY) }
  42. public var baseline: Constraint { return addConstraint(ConstraintAttributes.Baseline) }
  43. public var and: Constraint { return self }
  44. public var with: Constraint { return self }
  45. // MARK: initializer
  46. internal init(fromItem: ConstraintItem) {
  47. self.fromItem = fromItem
  48. self.toItem = ConstraintItem(view: nil, attributes: ConstraintAttributes.None)
  49. }
  50. // MARK: equalTo
  51. public func equalTo(other: ConstraintItem) -> Constraint {
  52. return constrainTo(other, relation: .Equal)
  53. }
  54. public func equalTo(other: View) -> Constraint {
  55. return constrainTo(other, relation: .Equal)
  56. }
  57. public func equalTo(other: Float) -> Constraint {
  58. return constrainTo(other, relation: .Equal)
  59. }
  60. public func equalTo(other: Double) -> Constraint {
  61. return constrainTo(Float(other), relation: .Equal)
  62. }
  63. public func equalTo(other: CGFloat) -> Constraint {
  64. return constrainTo(Float(other), relation: .Equal)
  65. }
  66. public func equalTo(other: Int) -> Constraint {
  67. return constrainTo(Float(other), relation: .Equal)
  68. }
  69. public func equalTo(other: UInt) -> Constraint {
  70. return constrainTo(Float(other), relation: .Equal)
  71. }
  72. public func equalTo(other: CGSize) -> Constraint {
  73. return constrainTo(other, relation: .Equal)
  74. }
  75. public func equalTo(other: CGPoint) -> Constraint {
  76. return constrainTo(other, relation: .Equal)
  77. }
  78. public func equalTo(other: EdgeInsets) -> Constraint {
  79. return constrainTo(other, relation: .Equal)
  80. }
  81. // MARK: lessThanOrEqualTo
  82. public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint {
  83. return constrainTo(other, relation: .LessThanOrEqualTo)
  84. }
  85. public func lessThanOrEqualTo(other: View) -> Constraint {
  86. return constrainTo(other, relation: .LessThanOrEqualTo)
  87. }
  88. public func lessThanOrEqualTo(other: Float) -> Constraint {
  89. return constrainTo(other, relation: .LessThanOrEqualTo)
  90. }
  91. public func lessThanOrEqualTo(other: Double) -> Constraint {
  92. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  93. }
  94. public func lessThanOrEqualTo(other: CGFloat) -> Constraint {
  95. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  96. }
  97. public func lessThanOrEqualTo(other: Int) -> Constraint {
  98. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  99. }
  100. public func lessThanOrEqualTo(other: UInt) -> Constraint {
  101. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  102. }
  103. public func lessThanOrEqualTo(other: CGSize) -> Constraint {
  104. return constrainTo(other, relation: .LessThanOrEqualTo)
  105. }
  106. public func lessThanOrEqualTo(other: CGPoint) -> Constraint {
  107. return constrainTo(other, relation: .LessThanOrEqualTo)
  108. }
  109. public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
  110. return constrainTo(other, relation: .LessThanOrEqualTo)
  111. }
  112. // MARK: greaterThanOrEqualTo
  113. public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint {
  114. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  115. }
  116. func greaterThanOrEqualTo(other: View) -> Constraint {
  117. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  118. }
  119. public func greaterThanOrEqualTo(other: Float) -> Constraint {
  120. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  121. }
  122. public func greaterThanOrEqualTo(other: Double) -> Constraint {
  123. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  124. }
  125. public func greaterThanOrEqualTo(other: CGFloat) -> Constraint {
  126. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  127. }
  128. public func greaterThanOrEqualTo(other: Int) -> Constraint {
  129. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  130. }
  131. public func greaterThanOrEqualTo(other: UInt) -> Constraint {
  132. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  133. }
  134. public func greaterThanOrEqualTo(other: CGSize) -> Constraint {
  135. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  136. }
  137. public func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
  138. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  139. }
  140. public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
  141. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  142. }
  143. // MARK: multiplier
  144. public func multipliedBy(amount: Float) -> Constraint {
  145. self.multiplier = amount
  146. return self
  147. }
  148. public func multipliedBy(amount: Double) -> Constraint {
  149. return self.multipliedBy(Float(amount))
  150. }
  151. public func multipliedBy(amount: CGFloat) -> Constraint {
  152. return self.multipliedBy(Float(amount))
  153. }
  154. public func multipliedBy(amount: Int) -> Constraint {
  155. return self.multipliedBy(Float(amount))
  156. }
  157. public func multipliedBy(amount: UInt) -> Constraint {
  158. return self.multipliedBy(Float(amount))
  159. }
  160. public func dividedBy(amount: Float) -> Constraint {
  161. self.multiplier = 1.0 / amount;
  162. return self
  163. }
  164. public func dividedBy(amount: Double) -> Constraint {
  165. return self.dividedBy(Float(amount))
  166. }
  167. public func dividedBy(amount: CGFloat) -> Constraint {
  168. return self.dividedBy(Float(amount))
  169. }
  170. public func dividedBy(amount: Int) -> Constraint {
  171. return self.dividedBy(Float(amount))
  172. }
  173. public func dividedBy(amount: UInt) -> Constraint {
  174. return self.dividedBy(Float(amount))
  175. }
  176. // MARK: priority
  177. public func priority(priority: Float) -> Constraint {
  178. self.priority = priority
  179. return self
  180. }
  181. public func priority(priority: Double) -> Constraint {
  182. return self.priority(Float(priority))
  183. }
  184. public func priority(priority: CGFloat) -> Constraint {
  185. return self.priority(Float(priority))
  186. }
  187. public func priority(priority: UInt) -> Constraint {
  188. return self.priority(Float(priority))
  189. }
  190. public func priority(priority: Int) -> Constraint {
  191. return self.priority(Float(priority))
  192. }
  193. public func priorityRequired() -> Constraint {
  194. return self.priority(1000.0)
  195. }
  196. public func priorityHigh() -> Constraint {
  197. return self.priority(750.0)
  198. }
  199. public func priorityLow() -> Constraint {
  200. return self.priority(250.0)
  201. }
  202. // MARK: offset
  203. public func offset(amount: Float) -> Constraint {
  204. self.offset = amount
  205. return self
  206. }
  207. public func offset(amount: Double) -> Constraint {
  208. return self.offset(Float(amount))
  209. }
  210. public func offset(amount: CGFloat) -> Constraint {
  211. return self.offset(Float(amount))
  212. }
  213. public func offset(amount: Int) -> Constraint {
  214. return self.offset(Float(amount))
  215. }
  216. public func offset(amount: UInt) -> Constraint {
  217. return self.offset(Float(amount))
  218. }
  219. public func offset(amount: CGPoint) -> Constraint {
  220. self.offset = amount
  221. return self
  222. }
  223. public func offset(amount: CGSize) -> Constraint {
  224. self.offset = amount
  225. return self
  226. }
  227. public func offset(amount: EdgeInsets) -> Constraint {
  228. self.offset = amount
  229. return self
  230. }
  231. // MARK: insets
  232. public func insets(amount: EdgeInsets) -> Constraint {
  233. self.offset = amount
  234. return self
  235. }
  236. // MARK: install
  237. public func install() -> Array<LayoutConstraint> {
  238. var installOnView: View? = nil
  239. if self.toItem.view != nil {
  240. installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
  241. if installOnView == nil {
  242. NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
  243. return []
  244. }
  245. } else {
  246. installOnView = self.fromItem.view?.superview
  247. if installOnView == nil {
  248. NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
  249. return []
  250. }
  251. }
  252. var layoutConstraints: Array<LayoutConstraint> = []
  253. let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
  254. let layoutToAttributes = self.toItem.attributes.layoutAttributes
  255. // get layout from
  256. let layoutFrom: View? = self.fromItem.view
  257. // get layout relation
  258. let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
  259. for layoutFromAttribute in layoutFromAttributes {
  260. // get layout to attribute
  261. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  262. // get layout constant
  263. var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
  264. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  265. // get layout to
  266. var layoutTo: View? = self.toItem.view
  267. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  268. layoutTo = installOnView
  269. }
  270. // create layout constraint
  271. let layoutConstraint = LayoutConstraint(
  272. item: layoutFrom!,
  273. attribute: layoutFromAttribute,
  274. relatedBy: layoutRelation,
  275. toItem: layoutTo,
  276. attribute: layoutToAttribute,
  277. multiplier: CGFloat(self.multiplier),
  278. constant: layoutConstant)
  279. // set priority
  280. layoutConstraint.priority = self.priority
  281. // set constraint
  282. layoutConstraint.constraint = self
  283. layoutConstraints.append(layoutConstraint)
  284. }
  285. installOnView?.addConstraints(layoutConstraints)
  286. self.installedOnView = installOnView
  287. return layoutConstraints
  288. }
  289. // MARK: uninstall
  290. public func uninstall() {
  291. if let view = self.installedOnView {
  292. #if os(iOS)
  293. var installedConstraints = view.constraints()
  294. #else
  295. var installedConstraints = view.constraints
  296. #endif
  297. var constraintsToRemove: Array<LayoutConstraint> = []
  298. for installedConstraint in installedConstraints {
  299. if let layoutConstraint = installedConstraint as? LayoutConstraint {
  300. if layoutConstraint.constraint === self {
  301. constraintsToRemove.append(layoutConstraint)
  302. }
  303. }
  304. }
  305. if constraintsToRemove.count > 0 {
  306. view.removeConstraints(constraintsToRemove)
  307. }
  308. }
  309. self.installedOnView = nil
  310. }
  311. // MARK: private
  312. private let fromItem: ConstraintItem
  313. private var toItem: ConstraintItem
  314. private var relation: ConstraintRelation?
  315. private var constant: Any?
  316. private var multiplier: Float = 1.0
  317. private var priority: Float = 1000.0
  318. private var offset: Any?
  319. private weak var installedOnView: View?
  320. private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
  321. if self.relation == nil {
  322. self.fromItem.attributes += attributes
  323. }
  324. return self
  325. }
  326. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
  327. if other.attributes != ConstraintAttributes.None {
  328. var toLayoutAttributes = other.attributes.layoutAttributes
  329. if toLayoutAttributes.count > 1 {
  330. var fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  331. if toLayoutAttributes != fromLayoutAttributes {
  332. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  333. return self
  334. }
  335. other.attributes = ConstraintAttributes.None
  336. }
  337. }
  338. self.toItem = other
  339. self.relation = relation
  340. return self
  341. }
  342. private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
  343. return constrainTo(ConstraintItem(view: other, attributes: ConstraintAttributes.None), relation: relation)
  344. }
  345. private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
  346. self.constant = other
  347. return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
  348. }
  349. private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
  350. self.constant = other
  351. return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
  352. }
  353. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
  354. self.constant = other
  355. return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
  356. }
  357. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
  358. self.constant = other
  359. return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
  360. }
  361. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
  362. self.constant = other
  363. return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
  364. }
  365. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  366. var closestCommonSuperview: View?
  367. var secondViewSuperview: View? = toView
  368. while closestCommonSuperview == nil && secondViewSuperview != nil {
  369. var firstViewSuperview = fromView
  370. while closestCommonSuperview == nil && firstViewSuperview != nil {
  371. if secondViewSuperview == firstViewSuperview {
  372. closestCommonSuperview = secondViewSuperview
  373. }
  374. firstViewSuperview = firstViewSuperview?.superview
  375. }
  376. secondViewSuperview = secondViewSuperview?.superview
  377. }
  378. return closestCommonSuperview
  379. }
  380. }
  381. private extension NSLayoutAttribute {
  382. private func snp_offsetForValue(value: Any?) -> CGFloat {
  383. // Float
  384. if let float = value as? Float {
  385. return CGFloat(float)
  386. }
  387. // Double
  388. else if let double = value as? Double {
  389. return CGFloat(double)
  390. }
  391. // UInt
  392. else if let int = value as? Int {
  393. return CGFloat(int)
  394. }
  395. // Int
  396. else if let uint = value as? UInt {
  397. return CGFloat(uint)
  398. }
  399. // CGFloat
  400. else if let float = value as? CGFloat {
  401. return float
  402. }
  403. // CGSize
  404. else if let size = value as? CGSize {
  405. if self == .Width {
  406. return size.width
  407. } else if self == .Height {
  408. return size.height
  409. }
  410. }
  411. // CGPoint
  412. else if let point = value as? CGPoint {
  413. if self == .Left || self == .CenterX {
  414. return point.x
  415. } else if self == .Top || self == .CenterY {
  416. return point.y
  417. } else if self == .Right {
  418. return -point.x
  419. } else if self == .Bottom {
  420. return -point.y
  421. }
  422. }
  423. // EdgeInsets
  424. else if let insets = value as? EdgeInsets {
  425. if self == .Left {
  426. return insets.left
  427. } else if self == .Top {
  428. return insets.top
  429. } else if self == .Right {
  430. return -insets.right
  431. } else if self == .Bottom {
  432. return -insets.bottom
  433. }
  434. }
  435. return CGFloat(0)
  436. }
  437. private func snp_constantForValue(value: Any?) -> CGFloat {
  438. // Float
  439. if let float = value as? Float {
  440. return CGFloat(float)
  441. }
  442. // Double
  443. else if let double = value as? Double {
  444. return CGFloat(double)
  445. }
  446. // UInt
  447. else if let int = value as? Int {
  448. return CGFloat(int)
  449. }
  450. // Int
  451. else if let uint = value as? UInt {
  452. return CGFloat(uint)
  453. }
  454. // CGFloat
  455. else if let float = value as? CGFloat {
  456. return float
  457. }
  458. // CGSize
  459. else if let size = value as? CGSize {
  460. if self == .Width {
  461. return size.width
  462. } else if self == .Height {
  463. return size.height
  464. }
  465. }
  466. // CGPoint
  467. else if let point = value as? CGPoint {
  468. if self == .Left || self == .CenterX {
  469. return point.x
  470. } else if self == .Top || self == .CenterY {
  471. return point.y
  472. } else if self == .Right {
  473. return point.x
  474. } else if self == .Bottom {
  475. return point.y
  476. }
  477. }
  478. // EdgeInsets
  479. else if let insets = value as? EdgeInsets {
  480. if self == .Left {
  481. return insets.left
  482. } else if self == .Top {
  483. return insets.top
  484. } else if self == .Right {
  485. return insets.right
  486. } else if self == .Bottom {
  487. return insets.bottom
  488. }
  489. }
  490. return CGFloat(0);
  491. }
  492. }