Constraint.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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: Int) -> Constraint {
  61. return constrainTo(Float(other), relation: .Equal)
  62. }
  63. public func equalTo(other: CGSize) -> Constraint {
  64. return constrainTo(other, relation: .Equal)
  65. }
  66. public func equalTo(other: CGPoint) -> Constraint {
  67. return constrainTo(other, relation: .Equal)
  68. }
  69. public func equalTo(other: EdgeInsets) -> Constraint {
  70. return constrainTo(other, relation: .Equal)
  71. }
  72. // MARK: lessThanOrEqualTo
  73. public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint {
  74. return constrainTo(other, relation: .LessThanOrEqualTo)
  75. }
  76. public func lessThanOrEqualTo(other: View) -> Constraint {
  77. return constrainTo(other, relation: .LessThanOrEqualTo)
  78. }
  79. public func lessThanOrEqualTo(other: Float) -> Constraint {
  80. return constrainTo(other, relation: .LessThanOrEqualTo)
  81. }
  82. public func lessThanOrEqualTo(other: Int) -> Constraint {
  83. return constrainTo(Float(other), relation: .LessThanOrEqualTo)
  84. }
  85. public func lessThanOrEqualTo(other: CGSize) -> Constraint {
  86. return constrainTo(other, relation: .LessThanOrEqualTo)
  87. }
  88. public func lessThanOrEqualTo(other: CGPoint) -> Constraint {
  89. return constrainTo(other, relation: .LessThanOrEqualTo)
  90. }
  91. public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
  92. return constrainTo(other, relation: .LessThanOrEqualTo)
  93. }
  94. // MARK: greaterThanOrEqualTo
  95. public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint {
  96. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  97. }
  98. func greaterThanOrEqualTo(other: View) -> Constraint {
  99. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  100. }
  101. public func greaterThanOrEqualTo(other: Float) -> Constraint {
  102. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  103. }
  104. public func greaterThanOrEqualTo(other: Int) -> Constraint {
  105. return constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  106. }
  107. public func greaterThanOrEqualTo(other: CGSize) -> Constraint {
  108. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  109. }
  110. public func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
  111. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  112. }
  113. public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
  114. return constrainTo(other, relation: .GreaterThanOrEqualTo)
  115. }
  116. // MARK: multiplier
  117. public func multipliedBy(amount: Float) -> Constraint {
  118. self.multiplier = amount
  119. return self
  120. }
  121. public func dividedBy(amount: Float) -> Constraint {
  122. self.multiplier = 1.0 / amount;
  123. return self
  124. }
  125. // MARK: priority
  126. public func priority(priority: Float) -> Constraint {
  127. self.priority = priority
  128. return self
  129. }
  130. public func priorityRequired() -> Constraint {
  131. return priority(1000.0)
  132. }
  133. public func priorityHigh() -> Constraint {
  134. return priority(750.0)
  135. }
  136. public func priorityLow() -> Constraint {
  137. return priority(250.0)
  138. }
  139. // MARK: offset
  140. public func offset(amount: Float) -> Constraint {
  141. self.offset = amount
  142. return self
  143. }
  144. public func offset(amount: Int) -> Constraint {
  145. self.offset = amount
  146. return self
  147. }
  148. public func offset(amount: CGPoint) -> Constraint {
  149. self.offset = amount
  150. return self
  151. }
  152. public func offset(amount: CGSize) -> Constraint {
  153. self.offset = amount
  154. return self
  155. }
  156. public func offset(amount: EdgeInsets) -> Constraint {
  157. self.offset = amount
  158. return self
  159. }
  160. // MARK: insets
  161. public func insets(amount: EdgeInsets) -> Constraint {
  162. self.offset = amount
  163. return self
  164. }
  165. // MARK: install
  166. public func install() -> Array<LayoutConstraint> {
  167. var installOnView: View? = nil
  168. if self.toItem.view != nil {
  169. installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
  170. if installOnView == nil {
  171. NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
  172. return []
  173. }
  174. } else {
  175. installOnView = self.fromItem.view?.superview
  176. if installOnView == nil {
  177. NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
  178. return []
  179. }
  180. }
  181. var layoutConstraints: Array<LayoutConstraint> = []
  182. let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
  183. let layoutToAttributes = self.toItem.attributes.layoutAttributes
  184. // get layout from
  185. let layoutFrom: View? = self.fromItem.view
  186. // get layout relation
  187. let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
  188. for layoutFromAttribute in layoutFromAttributes {
  189. // get layout to attribute
  190. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  191. // get layout constant
  192. var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
  193. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  194. // get layout to
  195. var layoutTo: View? = self.toItem.view
  196. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  197. layoutTo = installOnView
  198. }
  199. // create layout constraint
  200. let layoutConstraint = LayoutConstraint(
  201. item: layoutFrom!,
  202. attribute: layoutFromAttribute,
  203. relatedBy: layoutRelation,
  204. toItem: layoutTo,
  205. attribute: layoutToAttribute,
  206. multiplier: CGFloat(self.multiplier),
  207. constant: layoutConstant)
  208. // set priority
  209. layoutConstraint.priority = self.priority
  210. // set constraint
  211. layoutConstraint.constraint = self
  212. layoutConstraints.append(layoutConstraint)
  213. }
  214. installOnView?.addConstraints(layoutConstraints)
  215. self.installedOnView = installOnView
  216. return layoutConstraints
  217. }
  218. // MARK: uninstall
  219. public func uninstall() {
  220. if let view = self.installedOnView {
  221. #if os(iOS)
  222. var installedConstraints = view.constraints()
  223. #else
  224. var installedConstraints = view.constraints
  225. #endif
  226. var constraintsToRemove: Array<LayoutConstraint> = []
  227. for installedConstraint in installedConstraints {
  228. if let layoutConstraint = installedConstraint as? LayoutConstraint {
  229. if layoutConstraint.constraint === self {
  230. constraintsToRemove.append(layoutConstraint)
  231. }
  232. }
  233. }
  234. if constraintsToRemove.count > 0 {
  235. view.removeConstraints(constraintsToRemove)
  236. }
  237. }
  238. self.installedOnView = nil
  239. }
  240. // MARK: private
  241. private let fromItem: ConstraintItem
  242. private var toItem: ConstraintItem
  243. private var relation: ConstraintRelation?
  244. private var constant: Any?
  245. private var multiplier: Float = 1.0
  246. private var priority: Float = 1000.0
  247. private var offset: Any?
  248. private weak var installedOnView: View?
  249. private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
  250. if self.relation == nil {
  251. self.fromItem.attributes += attributes
  252. }
  253. return self
  254. }
  255. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
  256. if other.attributes != ConstraintAttributes.None {
  257. var toLayoutAttributes = other.attributes.layoutAttributes
  258. if toLayoutAttributes.count > 1 {
  259. var fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  260. if toLayoutAttributes != fromLayoutAttributes {
  261. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  262. return self
  263. }
  264. other.attributes = ConstraintAttributes.None
  265. }
  266. }
  267. self.toItem = other
  268. self.relation = relation
  269. return self
  270. }
  271. private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
  272. return constrainTo(ConstraintItem(view: other, attributes: ConstraintAttributes.None), relation: relation)
  273. }
  274. private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
  275. self.constant = other
  276. return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
  277. }
  278. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
  279. self.constant = other
  280. return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
  281. }
  282. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
  283. self.constant = other
  284. return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
  285. }
  286. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
  287. self.constant = other
  288. return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
  289. }
  290. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  291. var closestCommonSuperview: View?
  292. var secondViewSuperview: View? = toView
  293. while closestCommonSuperview == nil && secondViewSuperview != nil {
  294. var firstViewSuperview = fromView
  295. while closestCommonSuperview == nil && firstViewSuperview != nil {
  296. if secondViewSuperview == firstViewSuperview {
  297. closestCommonSuperview = secondViewSuperview
  298. }
  299. firstViewSuperview = firstViewSuperview?.superview
  300. }
  301. secondViewSuperview = secondViewSuperview?.superview
  302. }
  303. return closestCommonSuperview
  304. }
  305. }
  306. private extension NSLayoutAttribute {
  307. private func snp_offsetForValue(value: Any?) -> CGFloat {
  308. // Float
  309. if let float = value as? Float {
  310. return CGFloat(float)
  311. }
  312. // Int
  313. else if let int = value as? Int {
  314. return CGFloat(int)
  315. }
  316. // CGFloat
  317. else if let float = value as? CGFloat {
  318. return float
  319. }
  320. // CGSize
  321. else if let size = value as? CGSize {
  322. if self == .Width {
  323. return size.width
  324. } else if self == .Height {
  325. return size.height
  326. }
  327. }
  328. // CGPoint
  329. else if let point = value as? CGPoint {
  330. if self == .Left || self == .CenterX {
  331. return point.x
  332. } else if self == .Top || self == .CenterY {
  333. return point.y
  334. } else if self == .Right {
  335. return -point.x
  336. } else if self == .Bottom {
  337. return -point.y
  338. }
  339. }
  340. // EdgeInsets
  341. else if let insets = value as? EdgeInsets {
  342. if self == .Left {
  343. return insets.left
  344. } else if self == .Top {
  345. return insets.top
  346. } else if self == .Right {
  347. return -insets.right
  348. } else if self == .Bottom {
  349. return -insets.bottom
  350. }
  351. }
  352. return CGFloat(0)
  353. }
  354. private func snp_constantForValue(value: Any?) -> CGFloat {
  355. // Float
  356. if let float = value as? Float {
  357. return CGFloat(float)
  358. }
  359. // Int
  360. else if let int = value as? Int {
  361. return CGFloat(int)
  362. }
  363. // CGFloat
  364. else if let float = value as? CGFloat {
  365. return float
  366. }
  367. // CGSize
  368. else if let size = value as? CGSize {
  369. if self == .Width {
  370. return size.width
  371. } else if self == .Height {
  372. return size.height
  373. }
  374. }
  375. // CGPoint
  376. else if let point = value as? CGPoint {
  377. if self == .Left || self == .CenterX {
  378. return point.x
  379. } else if self == .Top || self == .CenterY {
  380. return point.y
  381. } else if self == .Right {
  382. return point.x
  383. } else if self == .Bottom {
  384. return point.y
  385. }
  386. }
  387. // EdgeInsets
  388. else if let insets = value as? EdgeInsets {
  389. if self == .Left {
  390. return insets.left
  391. } else if self == .Top {
  392. return insets.top
  393. } else if self == .Right {
  394. return insets.right
  395. } else if self == .Bottom {
  396. return insets.bottom
  397. }
  398. }
  399. return CGFloat(0);
  400. }
  401. }