View+SnapKit.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. public typealias View = UIView
  26. #else
  27. import AppKit
  28. public typealias View = NSView
  29. #endif
  30. /**
  31. Used to expose public API on views
  32. */
  33. public extension View {
  34. /// left edge
  35. public var snp_left: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Left) }
  36. /// top edge
  37. public var snp_top: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Top) }
  38. /// right edge
  39. public var snp_right: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Right) }
  40. /// bottom edge
  41. public var snp_bottom: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Bottom) }
  42. /// leading edge
  43. public var snp_leading: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Leading) }
  44. /// trailing edge
  45. public var snp_trailing: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Trailing) }
  46. /// width dimension
  47. public var snp_width: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Width) }
  48. /// height dimension
  49. public var snp_height: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Height) }
  50. /// centerX position
  51. public var snp_centerX: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterX) }
  52. /// centerY position
  53. public var snp_centerY: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterY) }
  54. /// baseline position
  55. public var snp_baseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Baseline) }
  56. #if os(iOS)
  57. /// first baseline position
  58. @available(iOS 8.0, *)
  59. public var snp_firstBaseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.FirstBaseline) }
  60. /// left margin
  61. @available(iOS 8.0, *)
  62. public var snp_leftMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeftMargin) }
  63. /// right margin
  64. @available(iOS 8.0, *)
  65. public var snp_rightMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.RightMargin) }
  66. /// top margin
  67. @available(iOS 8.0, *)
  68. public var snp_topMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TopMargin) }
  69. /// bottom margin
  70. @available(iOS 8.0, *)
  71. public var snp_bottomMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.BottomMargin) }
  72. /// leading margin
  73. @available(iOS 8.0, *)
  74. public var snp_leadingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeadingMargin) }
  75. /// trailing margin
  76. @available(iOS 8.0, *)
  77. public var snp_trailingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TrailingMargin) }
  78. /// centerX within margins
  79. @available(iOS 8.0, *)
  80. public var snp_centerXWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterXWithinMargins) }
  81. /// centerY within margins
  82. @available(iOS 8.0, *)
  83. public var snp_centerYWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterYWithinMargins) }
  84. #endif
  85. // top + left + bottom + right edges
  86. public var snp_edges: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Edges) }
  87. // width + height dimensions
  88. public var snp_size: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Size) }
  89. // centerX + centerY positions
  90. public var snp_center: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Center) }
  91. #if os(iOS)
  92. // top + left + bottom + right margins
  93. @available(iOS 8.0, *)
  94. public var snp_margins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Margins) }
  95. // centerX + centerY within margins
  96. @available(iOS 8.0, *)
  97. public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) }
  98. #endif
  99. /**
  100. Prepares constraints with a `ConstraintMaker` and returns the made constraints but does not install them.
  101. :param: closure that will be passed the `ConstraintMaker` to make the constraints with
  102. :returns: the constraints made
  103. */
  104. public func snp_prepareConstraints(file: String = __FILE__, line: UInt = __LINE__, @noescape closure: (make: ConstraintMaker) -> Void) -> [Constraint] {
  105. return ConstraintMaker.prepareConstraints(view: self, file: file, line: line, closure: closure)
  106. }
  107. /**
  108. Makes constraints with a `ConstraintMaker` and installs them along side any previous made constraints.
  109. :param: closure that will be passed the `ConstraintMaker` to make the constraints with
  110. */
  111. public func snp_makeConstraints(file: String = __FILE__, line: UInt = __LINE__, @noescape closure: (make: ConstraintMaker) -> Void) -> Void {
  112. ConstraintMaker.makeConstraints(view: self, file: file, line: line, closure: closure)
  113. }
  114. /**
  115. Updates constraints with a `ConstraintMaker` that will replace existing constraints that match and install new ones.
  116. For constraints to match only the constant can be updated.
  117. :param: closure that will be passed the `ConstraintMaker` to update the constraints with
  118. */
  119. public func snp_updateConstraints(file: String = __FILE__, line: UInt = __LINE__, @noescape closure: (make: ConstraintMaker) -> Void) -> Void {
  120. ConstraintMaker.updateConstraints(view: self, file: file, line: line, closure: closure)
  121. }
  122. /**
  123. Remakes constraints with a `ConstraintMaker` that will first remove all previously made constraints and make and install new ones.
  124. :param: closure that will be passed the `ConstraintMaker` to remake the constraints with
  125. */
  126. public func snp_remakeConstraints(file: String = __FILE__, line: UInt = __LINE__, @noescape closure: (make: ConstraintMaker) -> Void) -> Void {
  127. ConstraintMaker.remakeConstraints(view: self, file: file, line: line, closure: closure)
  128. }
  129. /**
  130. Removes all previously made constraints.
  131. */
  132. public func snp_removeConstraints() {
  133. ConstraintMaker.removeConstraints(view: self)
  134. }
  135. internal var snp_installedLayoutConstraints: [LayoutConstraint] {
  136. get {
  137. if let constraints = objc_getAssociatedObject(self, &installedLayoutConstraintsKey) as? [LayoutConstraint] {
  138. return constraints
  139. }
  140. return []
  141. }
  142. set {
  143. objc_setAssociatedObject(self, &installedLayoutConstraintsKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  144. }
  145. }
  146. }
  147. private var installedLayoutConstraintsKey = ""