View+SnapKit.swift 7.9 KB

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